这看起来有什么问题吗?
私有 int DBUpdate() {
DAL dal = new DAL();
string upd = "UPDATE [RPform] SET [ProjectName] = '@pname', [ProjectCode] = '@pcode', [Country] = @cnt, ";
upd += "[StartDate] = '@startdate', [FinishDate] = '@finishdate', [TotalParticipants] = @totpart, [ArrivalDate] = '@arrivedate', ";
upd += "[AirportTransfer] = @airtran, [AirportDate] = '@airdate', [AirportHours] = @airhour, [AirportMinutes] = @airmin, ";
upd += "[Problems] = '@problems', [FirstDayActivities] = '@fdayact' ";
upd += "WHERE [UserID]=@usid";
OleDbParameter[] parm = new OleDbParameter[] {
new OleDbParameter("@pname",projname.Text),
new OleDbParameter("@pcode",projcode.Text),
new OleDbParameter("@cnt",countries.SelectedIndex),
new OleDbParameter("@startdate",datestart.Text),
new OleDbParameter("@finishdate",datefinish.Text),
new OleDbParameter("@totpart",totalparticipants.Text),
new OleDbParameter("@arrivedate",datearrival.Text),
new OleDbParameter("@airtran",RadioButtonList1.SelectedValue),
new OleDbParameter("@airdate",dateairport.Text),
new OleDbParameter("@airhour",airporthours.SelectedIndex),
new OleDbParameter("@airmin",airportminutes.SelectedIndex),
new OleDbParameter("@problems",problems.Value),
new OleDbParameter("@fdayact",firstday.Value),
new OleDbParameter("@usid",user.ID)
};
return (dal.UpdateRow(upd,false,parm));
}
/// 它不会导致异常,但返回 0 受影响的行。 当从 MS Access 中执行相同的查询时,它工作正常。 因此我认为问题出在参数的处理上......但是什么? 谢谢
Sergio:显式设置 OleDbTypes 可以吗?
///whatever ...
new OleDbParameter("@problems",problems.Value),
new OleDbParameter("@fdayact",firstday.Value),
new OleDbParameter("@usid",user.ID)
};
//then telling each one what they will be ...
parm[0].OleDbType = OleDbType.VarWChar;
parm[1].OleDbType = OleDbType.VarWChar;
///
return (dal.UpdateRow(upd,false,parm));
private int DBUpdate()
{
DAL dal = new DAL();
string upd = "UPDATE [RPform] SET [ProjectName] = '@pname', [ProjectCode] = '@pcode', [Country] = @cnt, ";
upd += "[StartDate] = '@startdate', [FinishDate] = '@finishdate', [TotalParticipants] = @totpart, [ArrivalDate] = '@arrivedate', ";
upd += "[AirportTransfer] = @airtran, [AirportDate] = '@airdate', [AirportHours] = @airhour, [AirportMinutes] = @airmin, ";
upd += "[Problems] = '@problems', [FirstDayActivities] = '@fdayact' ";
upd += "WHERE [UserID]=@usid";
OleDbParameter[] parm = new OleDbParameter[] {
new OleDbParameter("@pname",projname.Text),
new OleDbParameter("@pcode",projcode.Text),
new OleDbParameter("@cnt",countries.SelectedIndex),
new OleDbParameter("@startdate",datestart.Text),
new OleDbParameter("@finishdate",datefinish.Text),
new OleDbParameter("@totpart",totalparticipants.Text),
new OleDbParameter("@arrivedate",datearrival.Text),
new OleDbParameter("@airtran",RadioButtonList1.SelectedValue),
new OleDbParameter("@airdate",dateairport.Text),
new OleDbParameter("@airhour",airporthours.SelectedIndex),
new OleDbParameter("@airmin",airportminutes.SelectedIndex),
new OleDbParameter("@problems",problems.Value),
new OleDbParameter("@fdayact",firstday.Value),
new OleDbParameter("@usid",user.ID)
};
return (dal.UpdateRow(upd,false,parm));
}
/// It causes no exceptions, but returns 0 rows affected. When same query executed from within MS Access it works fine. Hence I suppose the problem is sth with the handling of parameters ... but what? Thank you
Sergio: is this OK, for setting OleDbTypes explicitly?
///whatever ...
new OleDbParameter("@problems",problems.Value),
new OleDbParameter("@fdayact",firstday.Value),
new OleDbParameter("@usid",user.ID)
};
//then telling each one what they will be ...
parm[0].OleDbType = OleDbType.VarWChar;
parm[1].OleDbType = OleDbType.VarWChar;
///
return (dal.UpdateRow(upd,false,parm));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
检查user.ID的值是否设置正确。
在 upd 命令字符串中,您用引号将参数括起来,如下所示:
删除所有参数中的这些引号。
Check if the value of user.ID is being correctly set.
In your upd command string you are surrounding the parameters with quotes, like this:
Remove those quotes in all your parameters.
抱歉 emre,我忽略了 OleDb Provider。
如果您的 SQL 命令语法是用于 SQL 提供程序,则它是正确的。
因此,您的命令应如下所示:
然后,您应该像已有的那样添加 OleDb 参数,但必须小心按照它们在 SQL 命令中出现的顺序添加它们。
还有一件事,但这与您的问题无关:
您不应该连接字符串,因为该操作在性能方面不是很好。
相反,要很好地布置 SQL 命令,请尝试以下操作:
然后,要获取命令字符串,您只需要:
希望这可以帮助您。
Sorry emre, I overlooked the OleDb Provider.
Your SQL command syntax is correct if it was intended for an SQL provider.
So, your command should look like this:
Then you should add your OleDb parameters just like you already have, but you have to be careful to add them in the same order they appear in you SQL command.
One more thing, but that don't have to do with your problem:
you shouldn't concatenate strings, because that operation isn't very good in terms of performance.
Instead, to lay out your SQL command nicely, try this:
Then, to get your command string, you just need:
Hope that this could help you.
只是为了评论一下,如果您使用多行字符串文字,而不是连接,它会更具可读性(并且更容易复制/粘贴)。 另外,即使对于字符串参数,也不应该将参数名称括在单引号中(仅对文字使用单引号)。 因此,您的原始 SQL 可能类似于:
正如其他人指出的那样,OleDb 不使用命名参数,因此您实际上应该使用以下内容,确保按照 SQL 语句中出现的顺序将参数添加到 OleDbCommand:
Just to comment that rather than concatenating, it's more readable (and easier to copy/paste) if you use a multiline string literal. Also you should not enclose parameter names in single quotes, even for string parameters (use single quotes for literals only). So your original SQL could look something like:
As others have pointed out, OleDb does not use named parameters, so you should actually be using the following, ensuring that you add parameters to your OleDbCommand in the same order they appear in the SQL statement:
如果您在每个新的 OleDbParameter 中设置参数类型,则无需在 sql 上放置单引号,从而使指令不易出现输入错误。
If you set the parameter type in each new OleDbParameter you will not need to place single quotes on your sql making the instruction less prone to typing mistakes.
在这种情况下不要使用 StringBuilder。 将逐字字符串(@)与格式良好的 SQL 代码一起使用:
但我必须注意,+= 不会被优化! 简单的 + 将在编译时执行。 例如
将导致“ABC”没有性能命中
但
不会进行优化。
Don't use the StringBuilder in this case. Use verbatim strings(@) with well formatted SQL code:
But I have to note that += will not be optimized! The simple + will be performed at compile time. For example
will result in "ABC" no perf hit
However
will be not optimized.
顺便说一句,您可能希望使用 StringBuilder 而不是字符串来创建 upd。 每次使用 += 时,您都会丢弃旧字符串并创建一个新字符串。
尝试
并简单地使用它
By the way, you'll probably want to use a StringBuilder instead of a string to create upd. Every time you use += you're throwing away your old string a creating a new one.
Try
And to use it, simply
哎呀! 我解决了这个问题,听 Deltron 3030 ..它总是有效:)
这个 OleDbParameter 对象根据我传递的类型(作为 ctor 的对象)在内部设置其 oledbtype ..所以我传递了一个来自 TextBox (客户端)的整数侧面验证但仍然是文本,所以它崩溃了)..布尔值也是 radiobutton.Value ..它发送 True 但仍然是文本,因此它假定为字符串并将其封装在单引号中...但我看不到要给出什么名称这些参数因为它们是无名的...
抱歉,我讨厌阅读长篇文章,相反,我使用程序员的伟大美德之一,那就是懒惰..所以我在这里问:)
谢谢大家的时间和努力..祝你好运伙计们一切成功
Aight! I solved it listening to Deltron 3030 .. it always works :)
This OleDbParameter object sets its oledbtype internally, according to the type I have passed (as object to the ctor) .. so I have passed an integer coming from a TextBox (client side validated but still text, so it crashes) .. also the bool one was radiobutton.Value .. which sends True but still text so it assumes as string and encapsulates it in single quotes ... But I cannot see what names to give these params since they are nameless ...
Sorry I hate reading long articles, instead I use one of the great virtues of a programmer which is lazyness .. So I ask here :)
Thank you all for your time and efforts .. Wish you guys all the success