[英]How does JET OLEDB Parameters compare strings with text field in and Access DB
I have the following update query in C# using a JET OLEDB connection, connecting to a ms access DB file. 我使用JET OLEDB连接在C#中具有以下更新查询,连接到ms Access DB文件。 The query fails to change the fields, it runs correctly but just 0 rows changed.
查询无法更改字段,它可以正确运行,但仅更改了0行。
I think the problem is how parameters are processed and compared against the DB but have no idea how to fix it. 我认为问题在于如何处理参数并与数据库进行比较,但不知道如何解决。
The "User" column is set as text. “用户”列设置为文本。 I have an insert statement that works perfectly set up in the same fashion with the parameters.
我有一个insert语句,可以使用相同的方式完美地设置参数。
com.CommandText = "UPDATE [ExamMaster] SET [User] = (DLookup('LName', 'Users', 'ID' = '@correctUser') WHERE [User] = '@user'";
com.Parameters.AddWithValue("@correctUser", correctUser);
com.Parameters.AddWithValue("@user", userName);
If I do not use a parameter for the where clause and just insert it into the command string like so: 如果我不在where子句中使用参数,而是将其插入命令字符串中,如下所示:
WHERE [User] = '"+userName+"'";</code>
it will update the DB just fine. 它将更新数据库就好了。 What am I missing here?
我在这里想念什么? UPDATE: With or with single quotes makes no difference and rearranging the order of the parameters does not work either.
更新:带或带单引号没有区别,重新排列参数的顺序也不起作用。
The order matters. 顺序很重要。 I "think" in your query
user
is being called first before the correctUser
due to the DLOOKUP function. 由于DLOOKUP功能,我“认为”您的查询
user
首先在correctUser
user
之前被调用。
com.Parameters.AddWithValue("@user", userName);
com.Parameters.AddWithValue("@correctUser", correctUser);
You don't need to single quote parameters: 您不需要单引号参数:
WHERE [User] = @user";
and I'll guess that the DLOOKUP doesn't need the single quotes either, just [brackets] if the field name has a space or is a reserved word (which [User] might be). 并且我猜想DLOOKUP也不需要单引号,只要字段名称有空格或保留字([User]可能是)就只需[括号]。
You will need to change that a bit, try: 您将需要对此进行一些更改,请尝试:
OleDbConnection cn = new OleDbConnection(aconnectionstring);
cn.Open();
//testing
int correctUser = 1;
string userName = "1";
OleDbCommand com = new OleDbCommand();
com.Connection = cn;
//You cannot have a parameter in DLookUp
com.CommandText = "UPDATE [ExamMaster] SET [User] = " +
"DLookup('LName', 'Users', 'ID = " + correctUser + "') WHERE [User] = @user";
com.Parameters.AddWithValue("@user", userName);
//You must execute the query
com.ExecuteNonQuery();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.