使用MS ACCESS作为数据库的更新命令. [英] Update command using MS ACCESS as a Database.

查看:100
本文介绍了使用MS ACCESS作为数据库的更新命令.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MS Access作为数据库,
每当我尝试使用下面的给定语句进行更新时,都会在UPDATE中引发语法错误.

I am using MS Access as a DataBase,
whenever I''m trying to update using given below statements, it is throwing Syntax error in UPDATE.

OleDbConnection OCon = new OleDbConnection();
                OCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Database.mdb";
                string query;
                
                OleDbCommand cmdUpdate = new OleDbCommand();
                OCon.Open();
                query = "Update LIKey SET Desc='" + txtDesc.Text + "',Example='" + txtExample.Text + "' WHERE LIKeys=@LIKeys";
                cmdUpdate.Parameters.Clear();
                cmdUpdate.CommandText = query;
                cmdUpdate.CommandType = CommandType.Text;
                cmdUpdate.Parameters.Add("@LIKeys", OleDbType.LongVarChar);
                cmdUpdate.Parameters["@LIKeys"].Value = txtLIKey.Text;

                cmdUpdate.Connection = OCon;

                cmdUpdate.ExecuteNonQuery();
                OCon.Close();


如果我错了,请告诉我.

谢谢


Please tell me If I''m wrong.

Thanks

推荐答案

为什么您的某些SQL部分是在运行时构建的,而某些是参数的?请将所有参数都转换为参数,并使用Parameters.AddWithValue并重试.有关更新方法的一个很好的例子,请阅读此页面:

http://www.mikesdotnetting.com/Article/26/ASP.NET中带有MS-Access的参数查询 [
Why some of your SQL parts are built on runtime and some are Parameters ? Please convert all of them to parameters and also use Parameters.AddWithValue and try again. For a good example on update method read this page :

http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access[^]

Hope it helps.


尝试为
 query = "Update LIKey SET Desc=@desc,Example=@exp WHERE LIKeys=@LIKeys";
               // cmdUpdate.Parameters.Clear();
                cmdUpdate.CommandText = query;
                cmdUpdate.Connection = OCon;
                cmdUpdate.CommandType = CommandType.Text;
cmdUpdate.Parameters.AddWithValue("desc",txtDesc.Text);
cmdUpdate.Parameters.AddWithValue("exp",txtExample.Text);
cmdUpdate.Parameters.AddWithValue("LIKeys",txtLIKey.Text);
cmdUpdate.ExecuteNonQuery();


我在MS ACCESS中使用Desc作为列名,而在SQL Query中使用时,却将assim作为SQL中的排序变量它在UPDATE错误中抛出SYNTAX.


OleDbConnection OCon =新的OleDbConnection();
OCon.ConnectionString ="Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\\ Database.mdb";
字符串查询;

OleDbCommand cmdUpdate =新的OleDbCommand();
OCon.Open();
< pre lang ="c#"> query ="Update LIKey SET Desc =""+ txtDesc.Text +"'',Example =''"+ txtExample.Text +"''WHERE LIKeys = @ LIKeys; </pre>
< pre lang ="c#">//DESC是SQL的输入类型</pre>
cmdUpdate.Parameters.Clear();
cmdUpdate.CommandText =查询;
cmdUpdate.CommandType = CommandType.Text;
cmdUpdate.Parameters.Add("@ LIKeys",OleDbType.LongVarChar);
cmdUpdate.Parameters ["@ LIKeys"].Value = txtLIKey.Text;

cmdUpdate.Connection = OCon;

cmdUpdate.ExecuteNonQuery();
OCon.Close();
I was using Desc as a Column name in MS ACCESS and while using in SQL Query system was assiming as sorting variable in SQL and because of this it was throwing SYNTAX in UPDATE error.


OleDbConnection OCon = new OleDbConnection();
OCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Database.mdb";
string query;

OleDbCommand cmdUpdate = new OleDbCommand();
OCon.Open();
<pre lang="c#">query = "Update LIKey SET Desc=''" + txtDesc.Text + "'',Example=''" + txtExample.Text + "'' WHERE LIKeys=@LIKeys";</pre>
<pre lang="c#">// DESC IS A INBUILT TYPE IN SQL </pre>
cmdUpdate.Parameters.Clear();
cmdUpdate.CommandText = query;
cmdUpdate.CommandType = CommandType.Text;
cmdUpdate.Parameters.Add("@LIKeys", OleDbType.LongVarChar);
cmdUpdate.Parameters["@LIKeys"].Value = txtLIKey.Text;

cmdUpdate.Connection = OCon;

cmdUpdate.ExecuteNonQuery();
OCon.Close();


这篇关于使用MS ACCESS作为数据库的更新命令.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆