【问题标题】:How to search into multiple column(s) using single textbox如何使用单个文本框搜索多列)
【发布时间】:2018-12-03 14:09:06
【问题描述】:

我正在使用 SQL Server 数据库,我有一个包含多列的表 ([First Name], [Last Name]),我在 datagridview 中显示它。我的 UI 中还有一个文本框 (txtBoxSearch)。如何仅使用 1 个字符串在两列中进行搜索?

我在导航到用户屏幕时调用此方法以加载 datagridview 中的数据:

private void LoadData() {
        try {
            string sqlCommand = "SELECT * from tbl_user";
            con = new SqlConnection(connectionString);

            dataAdapter = new SqlDataAdapter(sqlCommand, connectionString);

            table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(table);
            dataGridProducts.DataSource = table;
        } catch (Exception ex) {
            MessageBox.Show(string.Format("An error occurred: {0}", ex.Message), "Error");
        }
    }

这是我的代码,它适用于第一次搜索,但再次搜索后它会显示找不到记录。

if (!string.IsNullOrWhiteSpace(txtBoxSearch.Text)) {
                try {
                    using (SqlConnection con = new SqlConnection(connectionString)) {
                        con.Open();
                        string sqlCommand = "SELECT * FROM tbl_user WHERE CONCAT([First Name], [Last Name]) LIKE '%" + txtBoxSearch.Text + "%'";
                        using (SqlCommand cmd = new SqlCommand(sqlCommand, con)) {

                            DataTable dt = new DataTable();
                            SqlDataAdapter ad = new SqlDataAdapter(cmd);
                            ad.Fill(dt);

                            if (dt.Rows.Count > 0) {
                                dataGridProducts.DataSource = dt;
                            } else {
                                MessageBox.Show("No record found.", "Error");
                            }
                        }
                    }
                } catch (Exception ex) {
                    MessageBox.Show(string.Format("An error occurred: {0}", ex.Message), "Error");
                } finally {
                    con.Close();
                }
            }

【问题讨论】:

标签: c# sql sql-server


【解决方案1】:
BindingSource.
table = new DataTable();

您已取出向我们展示为什么第二次无法工作的代码。


更新:

请在此处查看所有解决方案:Getting blank rows after setting DataGridView.DataSource

  1. DataGridView.AutoGenerateColumns = false
  2. DataPropertyNames 已设置
  3. DataGridView.ColumnCount = 0;因为新列可能会第二次隐藏数据。

【讨论】:

  • 是的,但这并不能解释为什么它不起作用。这应该是评论而不是答案。
  • 我应该把这条线放在哪里?我需要更改我的代码吗?
  • 你能添加代码吗,我怀疑问题与BindingSource有关。
  • @Zer0 我得到代码后会修复它。现在,由于缺少代码,我们无法判断。
  • @JeremyThompson 够公平的。根据代码,由于这条线dataGridUser.DataSource = table;,他甚至没有使用BindingSource
【解决方案2】:

您分配了 commandBuilder 但不使用它。请参考 SqlCommandBuilder 文档:

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder(v=vs.110).aspx

builder.GetUpdateCommand();

【讨论】:

  • 您好,我更新了问题,还包括了如何填充数据网格。谢谢
【解决方案3】:

我在这里看到了几个问题。我模拟了你的简单表格,并运行了下面的代码,它工作得很好。

string sqlCommand = "SELECT [Fist Name], [Last Name] FROM tbl_user WHERE [First Name] LIKE '%" +
txtBoxSearch.Text + "%' OR [Last Name] LIKE '%" + txtBoxSearch.Text + "%'";
table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
using(var dataAdapter = new SqlDataAdapter(sqlCommand, connectionString))
{
    dataAdapter.Fill(table);
}
dataGridUser.DataSource = table;

据我所知,您不需要SqlCommandBuilder。您也不需要BindingSource,也不需要使用它。

我在这里所做的唯一区别是处理 dataAdapter

例如,上面的代码在单击按钮时就可以正常工作。

我怀疑您没有发布所有代码。如果可能的话,请发布可以完全重现问题的代码。

【讨论】:

  • 您好,我更新了问题,还包括了如何填充数据网格。谢谢
  • @NicoTing 我看到了你的更新。您的代码应该可以工作。而我上面的代码确实如此。你的第二个代码块是如何调用的?也许它没有运行 UI 线程?但是,默认情况下,这应该抛出一个Exception。到目前为止,我没有足够的内容来重现您的问题。
  • 在导航到 UserUI 时调用 LoadData() 方法,并且在我的 txtboxsearch keydown 事件中,当用户按下回车键时,提供的代码就是我的确切代码。
  • @NicoTing 您的代码应该可以工作。也许在更新DataSource 后尝试在DataGridView 上拨打Refresh?还要验证 DataTable 中是否有行。
  • 谢谢@zer0,我现在就试试。
【解决方案4】:

发现我的代码没有问题,我的文本框 MultiLine 属性设置为 true。这就是为什么当我按下回车键时,之前的文本仍在文本框中。

顺便说一句,感谢所有分享他们想法的人:)

【讨论】:

  • 面部护理。请勾选此答案或您认为最有用的任何内容,以表明问题已得到解答。请记住,您可以随意对您认为有帮助的答案进行投票。
  • @Zer0 问题出在键盘前 30 厘米处。 Nico 我会因为告诉我们而推翻反对票。当焦点在文本框上时按回车键不会调用默认表单按钮。
  • 另一个问题,为什么 WHERE LIKE '%%' 会关心回车?
  • @JeremyThompson 它没有。问题只是一个空的DataTable。如果命令有问题,则有try catch。如果DataTable 为空,则检查带有MessageBox 错误。不检查命令文本的简单案例。
【解决方案5】:
private void txtsearch_TextChanged(object sender, EventArgs e)
{
    (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("ProductCode LIKE '{0}%' or ProductName LIKE '{1}%' or Type LIKE '{2}%' ", txtsearch.Text, txtsearch.Text, txtsearch.Text);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    相关资源
    最近更新 更多