gpt4 book ai didi

c# - 我正在运行一个 Web 应用程序并想用 C# 代码更新我的 SQL Server 用户配置文件数据库,但无论我做什么我都无法让它工作

转载 作者:太空宇宙 更新时间:2023-11-03 14:38:46 26 4
gpt4 key购买 nike

我正在尝试为我正在使用 ASP.NET 和 Bootstrap 创建的网站创建一个 MyAccount 页面,并在我们的对象后台运行 html 和 C#。我们正在使用 SQL Server 来运行我们的数据库,但我在更新更新时遇到了很多麻烦。我正在使用我让我的合作伙伴硬插入系统的测试用户配置文件,因为我还没有对帐户创建系统进行编程,只有登录。

问题是,当我运行我的更新代码时,SQL Server 数据库没有使用传递的信息进行更新。我没有收到任何异常,并且我知道我的连接字符串有效,因为我使用它登录并将数据库的内容拖到主屏幕上的文本框中。因此,我不知道为什么它不更新,我感到很失落。

我已经删除了部分连接字符串本身,因为该服务器不是我自己的,而且我不希望我教授的个人连接和登录凭据出现在互联网上。

我运行查询的调试标签告诉我每次我尝试更改 1 行受到影响的项目的状态变量。

我尝试更新的用户配置文件中存储了以下信息。

Username: MarshallE  
Email: Test1234
Password: Test1234!
First Name: Marshall
Last Name: Emmer
Properties Rented: Prime Place
Universities Attended Oklahoma State University
State: N/A (My partner just added this and didn't update the profile)

这是我用来尝试更新这个东西的代码:

protected void btnRegAcctUpdate_Click(object sender, EventArgs e)
{
// This unit's job is to update the database of the specified user with the info in the text boxes
// Make sure the password fields are the same
if (txtAcctPass1.Text == txtAcctPass2.Text)
{
lblAcctDebug.Text = "";

SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = "Server= (N/A ;Database=F19_groupRoss;User Id=groupRoss;Password = (N/A);";

string MyUpdateStatement;
MyUpdateStatement = "UPDATE dbo.UserID SET Email = @Email, Password = @Password, FirstName = @FirstName, LastName = @LastName, Universities = @University, Town = @Town, State = @State" + " WHERE UserID = @UserID";

SqlCommand MySqlCmd = new SqlCommand(MyUpdateStatement, MyConnection);

// These are the values we will be overriding the database with
MySqlCmd.Parameters.AddWithValue("@UserID", (string)Session["UserID"]);
MySqlCmd.Parameters.AddWithValue("@FirstName", txtAcctFirstName.Text);
MySqlCmd.Parameters.AddWithValue("@LastName", txtAcctLastName.Text);
MySqlCmd.Parameters.AddWithValue("@Email", txtAcctEmail.Text);
MySqlCmd.Parameters.AddWithValue("@Password", txtAcctPass1.Text);
MySqlCmd.Parameters.AddWithValue("@State", txtAcctState.Text);
MySqlCmd.Parameters.AddWithValue("@Town", txtAcctTown.Text);
MySqlCmd.Parameters.AddWithValue("@University", txtAcctUniversity.Text);

// Open the connection
MyConnection.Open();

int rows = MySqlCmd.ExecuteNonQuery();
lblDebug2.Text = "Rows affected: " + rows;

MyConnection.Close();
}
else
{
lblAcctDebug.Text = "Error: Please ensure the text in the new password and password confirmation boxes are identical";
}
}

如果有人能告诉我我做错了什么,我将不胜感激!

最佳答案

尝试像这样添加参数:

MysqlCmd.Parameters.Add(New Parameter(@FirstName, txtAcctFirstName.text));

关于c# - 我正在运行一个 Web 应用程序并想用 C# 代码更新我的 SQL Server 用户配置文件数据库,但无论我做什么我都无法让它工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58685504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com