C#,SQLreader/命令出现问题
我在使用 SqlDataReader
时遇到了一些问题:
public string GetVareNavn(string streg)
{
string navn = "";
SqlConnection myCon = DBcon.getInstance().conn();
string query =
"SELECT Navn FROM Vare WHERE Stregkode = ) Values('" + streg + "')";
myCon.Open();
SqlCommand com = new SqlCommand(query, myCon);
Console.WriteLine("navn: "+navn);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
navn = dr.GetString(1);
}
myCon.Close();
return navn;
}
它在 com.ExecutiveReader();
处抛出异常,异常是:
“)”附近的语法不正确。
我不知道为什么这个现在不起作用,因为我已经在另一个项目中使用了它。
I have some trouble with the SqlDataReader
:
public string GetVareNavn(string streg)
{
string navn = "";
SqlConnection myCon = DBcon.getInstance().conn();
string query =
"SELECT Navn FROM Vare WHERE Stregkode = ) Values('" + streg + "')";
myCon.Open();
SqlCommand com = new SqlCommand(query, myCon);
Console.WriteLine("navn: "+navn);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
navn = dr.GetString(1);
}
myCon.Close();
return navn;
}
It throws an exception at com.ExecutiveReader();
and the exception is:
Incorrect syntax near ')'.
I don't know why this one doesn't work right now, because I've used it in another project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的查询看起来像是从曾经是 INSERT 语句的内容复制而来;您不需要在语句末尾添加
VALUES...
子句。尝试将您的查询
更改为:然后修改此代码以使用参数:
Your query looks like it was copied from something that used to be an
INSERT
statement; you don't need theVALUES...
clause at the end of the statement. Try changing yourquery
to:Then modify this code to use the parameter:
它不起作用,因为您的 SQL 已损坏:
您希望
WHERE
子句做什么,以及您尝试使用什么值?看起来您的更新命令的复制/粘贴已损坏。此外,无论如何,您都不应该像这样将值放入 SQL 中 - 您应该使用参数化查询来避免 SQL 注入攻击(并避免格式问题等)。
It doesn't work because your SQL is broken:
What did you expect that
WHERE
clause to do, and what values are you trying to use? It looks like you've got a broken copy/paste from an update command.Additionally, you shouldn't put values into your SQL like that anyway - you should use parameterized queries to avoid SQL injection attacks (and to avoid formatting issues etc).
是的,它肯定会给。为什么将
Values
放入select
查询中?这是错误的语法,请立即尝试。Ya, surely it will give. Why you put the
Values
in yourselect
query? which is wrong syntax, Try Now.