SQLException ## 附近的语法不正确
当我尝试选择一些值时,我收到了 sqlException 。 错误:“@navn”附近的语法不正确。我四处寻找与我类似的其他问题,但找不到任何对我有价值的东西。
我的代码:
public List<Vare> findvareAdvanced(string varenavn)
{
Vare v = new Vare();
List<Vare> lv = new List<Vare>();
SqlConnection myCon = DBcon.getInstance().conn();
string query = string.Format("SELECT * FROM Vare WHERE Navn LIKE %@navn%");
myCon.Open();
SqlCommand com = new SqlCommand(query, myCon);
com.Parameters.AddWithValue("navn", varenavn);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
v.Stregkode = dr.GetString(0);
v.Navn = dr.GetString(1);
v.Pris = dr.GetDecimal(2);
v.Varegruppenr = dr.GetInt32(3);
lv.Add(v);
}
myCon.Close();
return lv;
}
I'm getting a sqlexception while im trying to select some values.
The error: Incorrect syntax near '@navn'. I've looked around to find other questions similar to mine, but I can't find anything worth to me.
My code:
public List<Vare> findvareAdvanced(string varenavn)
{
Vare v = new Vare();
List<Vare> lv = new List<Vare>();
SqlConnection myCon = DBcon.getInstance().conn();
string query = string.Format("SELECT * FROM Vare WHERE Navn LIKE %@navn%");
myCon.Open();
SqlCommand com = new SqlCommand(query, myCon);
com.Parameters.AddWithValue("navn", varenavn);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
v.Stregkode = dr.GetString(0);
v.Navn = dr.GetString(1);
v.Pris = dr.GetDecimal(2);
v.Varegruppenr = dr.GetInt32(3);
lv.Add(v);
}
myCon.Close();
return lv;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试,
Try,
忘记了'?
forgot the ' ?
如果您想要字符串的字面翻译,请在字符串前添加
@
符号。这将有助于处理转义字符这个问题也可能有帮助
If you want a literal translation of the string, add an
@
symbol before the string. This will help handle escape charactersThis question might also help