c# - 如何使用单个文本框搜索多列)

标签 c# sql sql-server

我正在使用 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();
                }
            }

最佳答案

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

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

例如,上面的代码在单击按钮时工作得很好。

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

关于c# - 如何使用单个文本框搜索多列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51016027/

相关文章:

php - 当第一个表中的列值与第二个表中的列值匹配时显示一个表中的数据

sql - 嵌套游标、多个结果集

sql - 如何在子查询中引用临时表?

c# - EXCEL 的 OLEDB - 删除表 [工作表名称$] - 不会删除工作表

c# - 如何在长时间的服务器进程中显示信息丰富的实时进度数据

mysql - MySQL如何找出连续行中日期的差异并分别更新表列?

MYSQL 如何检查表是否为 NULL,而不是 0

c# - 将 System.Diagnostics.ProcessStartInfo StandardOutput 读取为字节而不是字符

c# - .Net 中的位图保存是否以不正确的格式保存图像?

mysql - 仅选择最近的记录