我正在运行一个Web应用程序,并希望使用C#代码更新我的SQL Server用户配置文件数据库,但是无论如何我都无法使其正常工作

我正在尝试为ASP.NET和Bootstrap创建的网站创建Myaccount页面,并在后台为我们的对象运行html和C#。我们正在使用SQL Server来运行数据库,但在进行更新时遇到了很多麻烦。我使用的是测试用户个人资料,我的搭档已硬插入系统中,因为我尚未对帐户创建系统进行编程,只能对登录进行编程。

问题在于,当我运行更新代码时,SQL Server数据库不会使用传递的信息进行更新。我没有收到任何异常,并且我知道我的连接字符串可以正常工作,因为我使用它来登录并将数据库的内容拖到主屏幕上的文本框中。因此,我不知道为什么它没有更新,我感到非常迷茫。

我已删除了连接字符串本身的部分内容,因为该服务器不是我自己的服务器,并且我不希望教授的个人连接和登录凭据在Internet上。

我运行查询的调试标签会告诉我每次尝试更改已影响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";
    }
}

如果有人可以告诉我我在做什么错,我将非常感谢!

cat0927 回答:我正在运行一个Web应用程序,并希望使用C#代码更新我的SQL Server用户配置文件数据库,但是无论如何我都无法使其正常工作

尝试像这样添加参数:

MysqlCmd.Parameters.Add(New Parameter(@FirstName,txtAcctFirstName.text));
本文链接:https://www.f2er.com/3169783.html

大家都在问