How Access updates the database using parameter Methods

Source: Internet
Author: User

I encountered this problem yesterday afternoon, but the debugging was always successful, but the data was not updated. After searching on the Internet, there are still few such items, probably because the Access database is rarely used, or because most people do not need parameter methods during the update process, let me record my experiences today. You can also make reference later.

Original error code:

   1:  StringBuilder strSql = new StringBuilder();
   2:  strSql.Append("update news_Table set ");
   3:  strSql.Append("NewsTitle=@NewsTitle,");
   4:  strSql.Append("NewsContent=@NewsContent,");
   5:  strSql.Append("NewsReporter=@NewsReporter");
   6:              
   7:  strSql.Append(" where NewsID=@NewsID ");
   8:  OleDbParameter[] parameters = {
   9:          new OleDbParameter("@NewsID", OleDbType.Integer,4),
  10:          new OleDbParameter("@NewsTitle", OleDbType.VarChar),
  11:          new OleDbParameter("@NewsContent", OleDbType.VarChar),
  12:          new OleDbParameter("@NewsReporter", OleDbType.VarChar)};
  13:  parameters[0].Value = model.NewsID;
  14:  parameters[1].Value = model.NewsTitle;
  15:  parameters[2].Value = model.NewsContent;
  16:  parameters[3].Value = model.NewsReporter;
  17:              
  18:  return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);

Because this code is automatically generated using the dynamic soft code generator, I am sure the code is correct, but the problem is that I modified the parameters and the automatically generated code contains the updatable ID, the ID in my database is automatically increased, so it cannot be updated. I deleted the ID directly and changed it to the error code above. In this way, the problem arises. No matter how debugging is done, it will not help, and it will never be updated to the database.

After a long time, I updated the database in a different way, that is, using the ++ Code directly without passing the @ parameter.

++ Code:

   1:  string strSql;
   2:  strSql="update news_Table set NewsTitle="+model.NewsTitle+",NewsContent="+model.NewsContent+",NewsReporter="+
   3:          model.NewsReporter+"  where NewsID="+model.NewsID;
   4:  return DbHelperOleDb.ExecuteSql(strSql);

This method can be used to smoothly update the data in the database.

These results make me crazy. I don't know what to do. I can't figure out why the parameter transfer method is not good, but simply write it. Next, I searched this information on Google frantically, but I found that there were not many logs of the same problems as I found. But there is still a solution to solve this problem, that is, the order of parameter values is the same as the input time. The correct code is posted below:

The correct parameter passing code:

   1:  StringBuilder strSql = new StringBuilder();
   2:  strSql.Append("update news_Table set ");
   3:  strSql.Append("NewsTitle=@NewsTitle,");
   4:  strSql.Append("NewsContent=@NewsContent,");
   5:  strSql.Append("NewsReporter=@NewsReporter");
   6:  strSql.Append(" where NewsID=@NewsID ");
   7:   
   8:  OleDbParameter[] parameters = {
   9:          new OleDbParameter("@NewsTitle", OleDbType.VarChar),
  10:          new OleDbParameter("@NewsContent", OleDbType.VarChar),
  11:          new OleDbParameter("@NewsReporter", OleDbType.VarChar),
  12:          new OleDbParameter("@NewsID", OleDbType.Integer,4)};
  13:   
  14:  parameters[0].Value = model.NewsTitle;
  15:  parameters[1].Value = model.NewsContent;
  16:  parameters[2].Value = model.NewsReporter;
  17:  parameters[3].Value = model.NewsID;           
  18:   
  19:  return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.