在数据库中搜索,在DataGridView中输出

时间:2014-03-25 13:40:32

标签: c# sql sql-server winforms

我有一个包含2个表的数据库。现在我想在我的数据库中搜索值。

enter image description here

我使用这个GUI所以"用户"可以向文本框提供信息,搜索的值应显示在我的Datagridview

我使用此代码:

    private void TB_MSGHeadline_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (TB_MSGHeadline.Text.Equals(""))
            {

            }
            else
            {

                    DataView dv = new DataView(table);
                    dv.RowFilter = string.Format("MessageHeadline LIKE '%{0}%'", TB_MSGHeadline.Text);

                    dataGridView1.DataSource = dv;

            }
        }
        catch (Exception i)
        {
            MessageBox.Show("" + i);
        }
    }

填写DataGridview

        private void GetData(string selectCommand)
    {
        //Creating a new data adapter based on the specified query.
        dataAdapter = new SqlDataAdapter(selectCommand, ConnectionString);

        //Create a command builder to generate SQL update, insert, and 
        //delete commands based on selectCommand. These are used to
        //update the Database.
        SqlCommandBuilder CommandBuilder = new SqlCommandBuilder();

        //Populate a new data table and bind it to the BindingSource
        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        dataAdapter.Fill(table);
        BindingSource1.DataSource = table;
    }

        private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.DataSource = BindingSource1;
        GetData("SELECT * FROM Tabelle1");
    }

(在视频中找到但不适用于我的问题)

但是我得到了错误,即行" MessageHeadline"没找到 enter image description here

所以我想知道,如何对我的SQL数据库进行LIKE搜索?

0 个答案:

没有答案