更新查询:值在错误的列中更新

时间:2014-02-01 15:47:18

标签: c# ms-access ado.net

我正在成功更新表格,但由于某种原因,这些字段处于混乱状态。 使用MS Access Database 2007处理Visual Studio 2010 C#。

这是我的代码

        OleDbCommand cm = new OleDbCommand();
        cm.Connection = AccessConnection();

        cm.Parameters.AddWithValue("vContactId", c.ContactId);
        cm.Parameters.AddWithValue("vFirstName", c.FirstName);
        cm.Parameters.AddWithValue("vLastName", c.LastName);
        cm.Parameters.AddWithValue("vAddress", c.Address);
        cm.Parameters.AddWithValue("vCity", c.City);
        cm.Parameters.AddWithValue("vState", c.State);
        cm.Parameters.AddWithValue("vCountry", c.Country);
        cm.Parameters.AddWithValue("vPinCode", c.PinCode);
        cm.Parameters.AddWithValue("vMobilePhone", c.MobilePhone);
        cm.Parameters.AddWithValue("vFax", c.Fax);
        cm.Parameters.AddWithValue("vOfficePhone", c.OfficePhone);
        cm.Parameters.AddWithValue("vHomePhone", c.HomePhone);
        cm.Parameters.AddWithValue("vCompany", c.Company);
        cm.Parameters.AddWithValue("vDesignation", c.Designation);
        cm.Parameters.AddWithValue("vEmail", c.Email);
        cm.Parameters.AddWithValue("vNotes", c.Notes);
        cm.Parameters.AddWithValue("vCategoryId", c.CategoryId);
        cm.Parameters.AddWithValue("vDtUpdated", c.dtUpdated);
        cm.Parameters.AddWithValue("vTitle", c.title);

        string q = "UPDATE tblContacts SET FirstName = vFirstName, LastName = vLastName, Address = vAddress, City = vCity, " +
                    "State = vState, Country = vCountry, PinCode = vPinCode, MobilePhone = vMobilePhone, Fax = vFax, " +
                    "OfficePhone = vOfficePhone, HomePhone = vHomePhone, Company = vCompany, Designation = vDesignation, " +
                    "Email = vEmail, Notes = vNotes, DtUpdated = vDtUpdated, CategoryId = vCategoryId, Title = vTitle" +
                    " WHERE ContactId=vContactId";
        cm.CommandText = q;

        if (!Exist(c))
        {
            cm.Connection.Open();
            cm.ExecuteNonQuery();
            cm.Connection.Close();
        }

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

OleDb 忽略参数的名称。它识别的只是参数在CommandText中出现的顺序。 (参考:here

您需要更改.AddWithValue语句的顺序,使其与CommandText中参数的显示顺序完全匹配。