DataGrid在WPF中搜索数据库中的值

时间:2015-09-04 13:28:07

标签: c# sql sql-server wpf datagrid

我目前正在使用Datagrid开发一个Search TextBox,以便在C#和C#中查看数据库中的数据。 WPF应用程序。但我需要在查询中使用一些内部联接。问题是当我在texbox中输入一些文本(名称)时,我会收到一个消息:在cad附近不正确的sintaxe。我在sql server中执行此查询,它工作正常,但在c#中没有。 这是我的代码:

private void txt_SearchCad(object sender, TextChangedEventArgs e)
        {
            _con = new SqlConnection(_strConn);

            try
            {
                _con.Open();
                string query = "select cad.id AS ID, cad.name AS Name, cad.date AS Date, cad.age AS Age,"+
                                "agegroup.name AS AgeGroup,activity.name AS Activity, professional.name AS Professional"+ 
                                "from cad inner join agegroup on cad.fokagegroup = agegroup.id"+
                                "inner join activity on cad.fokativity = activity.id"+
                                "inner join professional on cad.fokprofissional = profissional.id";
                if (txtSearchName.Text != "")
                {
                    query += " where cad.name like '%" + txtSearchName.Text + "%'";
                }
                _cmd = new SqlCommand(query, _con);
                _cmd.ExecuteNonQuery();

                _adp = new SqlDataAdapter(_cmd);
                _dt = new DataTable("cad");
                _adp.Fill(_dt);
                dgList.ItemsSource = _dt.DefaultView;
                _adp.Update(_dt);

                _con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

0 个答案:

没有答案