语法错误:'Student'运算符后缺少操作数

时间:2022-10-07 22:34:44

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 ......我想要它,所以我只需键入一个他们名字的字母就可以搜索人物我该怎么办?

4 个解决方案

#1


2  

You need to fill your DataTable first 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

或者在没有RowFilter的SQL中过滤

var cmd = new SqlCommand("SELECT * FROM Student WHERE Forename LIKE @forename", sc);
cmd.Parameters.AddWithValue("@forename", String.Concat("%", SearchFor, "%"));
adapter.SelectCommand = cmd;

#2


1  

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.

但请注意,用户可以输入一些字符,如%,'等。

#3


0  

please change as below..

请更改如下..

dt.DefaultView.RowFilter ="Forename LIKE '%" + SearchFor + "%'"

#4


0  

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文档。

#1


2  

You need to fill your DataTable first 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

或者在没有RowFilter的SQL中过滤

var cmd = new SqlCommand("SELECT * FROM Student WHERE Forename LIKE @forename", sc);
cmd.Parameters.AddWithValue("@forename", String.Concat("%", SearchFor, "%"));
adapter.SelectCommand = cmd;

#2


1  

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.

但请注意,用户可以输入一些字符,如%,'等。

#3


0  

please change as below..

请更改如下..

dt.DefaultView.RowFilter ="Forename LIKE '%" + SearchFor + "%'"

#4


0  

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文档。