在C#中用于Access数据库选择的SQL命令 [英] SQL command for access database select in c#

查看:67
本文介绍了在C#中用于Access数据库选择的SQL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于从C#连接.mdb数据库,如何传递变量以进行选择,例如:

对于名为"a"的字符串字段,如果我直接将字符串传递给它,就可以了,例如

for connecting a .mdb database from C#, how to pass a variable for selecting, e.g.:

for a string field called "a", if I directly pass a string to it, it is OK, e.g.

OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM mytable where a= 'hello' " , con); 



但是现在我想将字符串变量str_var传递给它,怎么办?

下一个是错误的,



but now I want to pass a string variable str_var to it, how to do?

the following one is wrong,

OleDbCommand cmd = new OleDbCommand("SELECT * FROM mytable where a = " + str_var , con);



以下一项也不正确:



the following one is also not correct:

OleDbCommand cmd = new OleDbCommand("SELECT * FROM mytable where a = @str_var "  , con);



正确的是什么?

谢谢!



what would be the correct one?

thanks!

推荐答案

OleDbCommand cmd = new OleDbCommand("SELECT * FROM mytable where a = ''" + str_var + "''" , con);



我想这就是你的意思.



I think that''s what you mean.


这篇关于在C#中用于Access数据库选择的SQL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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