2015-01-15 17 views
0

在我的代码,我想搜索所有领域,在短短一个文本框,但我得到这个错误:了条件,预计,不久的“或”在上下文中指定非布尔类型的表达式

An expression of non-boolean type specified in a context where a condition is expected, near 'OR'.

cn.Open() 

With cmd 
    .Connection = cn 
    .CommandText = "SELECT * FROM FactandStaff WHERE Familyname OR Firstname like '%" & txtsearch.Text & "%'" 
End With 

da.SelectCommand = cmd 

dt.Clear() 
da.Fill(dt) 

dg2.DataSource = dt 

cn.Close() 
+2

你不能说'col1或col2 =/LIKE something' - 你需要把它改成'WHERE FamilyName LIKE'%something%'或者名字LIKE'%something%''。你真的真的需要阅读SQL注入和参数化查询。此代码正在乞求成为下一个关于数据库通过其网站被利用的新闻故事... –

+0

@Aaron Bertrand-Tnx先生它运作良好。我期待更多地了解SQL。 –

回答

0

CommandText应该是:

SELECT * FROM FactandStaff WHERE Familyname LIKE '%" & txtsearch.Text & "%' OR Firstname LIKE '%" & txtsearch.Text & "%' 

所以就变成:

With cmd 
    .Connection = cn 
    .CommandText = "SELECT * FROM FactandStaff WHERE Familyname LIKE '%" & txtsearch.Text & "%' OR Firstname LIKE '%" & txtsearch.Text & "%'" 
End With 
相关问题