在访问数据库更新查询工作不C#.NET数据库、工作、NET

2023-09-04 01:05:43 作者:哭泣的笑容

我工作的一个数据库管理系统。我有更新的用户配置文件的一个简单的任务。我创建了一个asp.net页面文本框和保存按钮。添加文本后,我点击保存按钮。在code的按钮

I am working on a database management system. I have a simple task of updating user profile. I created an asp.net page with textboxes and a save button. After adding the text I click on the save button. The code for the button is

protected void Button1_Click(object sender, EventArgs e)
    {
        string firstName = TextBox2.Text;
        string lastName = TextBox1.Text;
        string sCourse = TextBox3.Text;
        string sTelephone = TextBox4.Text;
        string sAddress = TextBox5.Text;
        string sEmail = TextBox6.Text;
        string Gender = TextBox7.Text;
        string user = User.Identity.Name;

        OleDbConnection oleDBConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\ASPNetDB.accdb");
        string sqlQuerry = "UPDATE aspnet_Users SET firstName=@firstName, lastName=@lastName, Gender=@Gender, Address=@Address, Telephone=@Telephone, Course=@Course, Email=@email WHERE UserName=@UserName";

        OleDbCommand cmd = new OleDbCommand(sqlQuerry, oleDBConn);

        cmd.Parameters.AddWithValue("@UserName", User.Identity.Name);
        cmd.Parameters.AddWithValue("@firstName", firstName);
        cmd.Parameters.AddWithValue("@lastName", lastName);
        cmd.Parameters.AddWithValue("@Course", sCourse);
        cmd.Parameters.AddWithValue("@Telephone", sTelephone);
        cmd.Parameters.AddWithValue("@Address", sAddress);
        cmd.Parameters.AddWithValue("@Gender", Gender);
        cmd.Parameters.AddWithValue("@Email", sEmail);

        oleDBConn.Open();
        cmd.ExecuteNonQuery();
    }

但没有任何反应。数据库未更新。是code正确吗?

But nothing happens. The database is not updated. Is the code correct?

推荐答案

添加参数值的顺序相同的参数名称出现在更新语句。

Add the parameter values in the same order as the parameter names appear in the UPDATE statement.

cmd.Parameters.AddWithValue("@firstName", firstName);
cmd.Parameters.AddWithValue("@lastName", lastName);
cmd.Parameters.AddWithValue("@Gender", Gender);
cmd.Parameters.AddWithValue("@Address", sAddress);
cmd.Parameters.AddWithValue("@Telephone", sTelephone);
cmd.Parameters.AddWithValue("@Course", sCourse);
cmd.Parameters.AddWithValue("@Email", sEmail);
cmd.Parameters.AddWithValue("@UserName", User.Identity.Name);

的OleDb与Access不会注意参数的的名字的,只是它们的顺序。

OleDb with Access does not pay attention to the parameter names, only their order.