wpf 语法错误:“Student”运算符后缺少操作数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24778491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error: Missing operand after 'Student' operator
提问by BenNixon
This is the code I have:
这是我的代码:
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection sc = new SqlConnection(sConn))
{
sc.Open();
string SearchFor = txtSearch.Text;
using (SqlDataAdapter adapter = new SqlDataAdapter())
{
DataTable dt = new DataTable();
grdData.ItemsSource = dt.DefaultView.RowFilter = string.Concat("SELECT * FROM Student WHERE Forename LIKE '%", SearchFor, "%'");
}
When I run the code I get:
当我运行代码时,我得到:
"Syntax error: Missing operand after 'Student' operator."
“语法错误:'Student' 运算符后缺少操作数。”
I'm using WPF... I want it so I can search for people by just typing a letter of their name what can I do?
我正在使用 WPF...我想要它,这样我就可以通过键入他们名字的字母来搜索人我能做什么?
回答by DavidG
In your filter, you only need to specify the WHERE clause, it's not a full SQL SELECT statement that is needed. So it should look like this:
在您的过滤器中,您只需要指定WHERE 子句,它不需要完整的 SQL SELECT 语句。所以它应该是这样的:
grdData.ItemsSource = dt.DefaultView.RowFilter = string.Concat("Forename LIKE '%", SearchFor, "%'");
Be aware though, that a user could enter some characters like %, 'etc.
要知道,虽然,一个用户可以输入像一些字符%,'等等。
回答by dkozl
You need to fill your DataTablefirst with some data and then you can filter results
您需要先填写DataTable一些数据,然后才能过滤结果
using (SqlConnection sc = new SqlConnection(sConn))
{
sc.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter())
{
adapter.SelectCommand = new SqlCommand("SELECT * FROM Student", sc);
var dataset = new DataSet();
adapter.Fill(dataset);
var dt = dataset.Tables[0];
var dv = dt.DefaultView;
dv.RowFilter = String.Concat("[Forename] LIKE '%", SearchFor, "%'");
grdData.ItemsSource = dv;
}
}
or filter in SQL without RowFilter
或在 SQL 中过滤而不 RowFilter
var cmd = new SqlCommand("SELECT * FROM Student WHERE Forename LIKE @forename", sc);
cmd.Parameters.AddWithValue("@forename", String.Concat("%", SearchFor, "%"));
adapter.SelectCommand = cmd;
回答by Ashok Rathod
please change as below..
请更改如下..
dt.DefaultView.RowFilter ="Forename LIKE '%" + SearchFor + "%'"
回答by jpsfs
The "RowFilter" query is not a complete SQL query but rather a subset of a SQL query. In your case you want something like:
“RowFilter”查询不是完整的 SQL 查询,而是 SQL 查询的子集。在你的情况下,你想要这样的东西:
grdData.ItemsSource = dt.DefaultView.RowFilter = String.Format("[Forename] LIKE '%{0}%'", SearchFor);
For future reference: MSDN Documentation.
供将来参考:MSDN 文档。

