【问题标题】:C# & SQL Server : searching all database columns and populating datagridC# & SQL Server:搜索所有数据库列并填充数据网格
【发布时间】:2017-06-03 00:19:28
【问题描述】:

我正在编写一个 C# 应用程序,但我一直在搜索数据库和填充数据网格视图。但是我想将它与命令生成器一起使用。

问题是,我需要对数据库中的所有列进行搜索。我认为使用 ORLIKE 语句可以做到这一点。但相反,我得到无效的语法或搜索中不存在列名。

有人知道解决办法吗?

我现在的.cs

private void btnSearchJob_Click(object sender, EventArgs e)
{
    try
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = (@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MTR_Database;Integrated Security=True");

        string selectQuery = "SELECT * FROM dbo.[" + cmbJobName.Text + "] WHERE ([Job Name] LIKE " +txtSearchJob.Text+ " OR [Manufacturer] LIKE " +txtSearchJob.Text+ ")";

        // DataAdapter
        myDA = new SqlDataAdapter(selectQuery, con);

        // SqlCommand
        SqlCommand myCMD = new SqlCommand(selectQuery, con);

        // DataAdapter to Command
        myDA.SelectCommand = myCMD;

        // Define Datatable
        myDT = new DataTable();

        // Command Builder (IS GOD!)
        SqlCommandBuilder cb = new SqlCommandBuilder(myDA);

        // Teach Command builder to be a boss!
        myDA.UpdateCommand = cb.GetUpdateCommand();
        myDA.InsertCommand = cb.GetInsertCommand();
        myDA.DeleteCommand = cb.GetDeleteCommand();

        // Fill the DataTable with DataAdapter information
        myDA.Fill(myDT);

        // Fill DataTable with Database Schema
        myDA.FillSchema(myDT, SchemaType.Source);

        // Bind The Data Table to the DataGrid
        dataGridView1.DataSource = myDT;

        // AutoSize Datagrid Rows and Colums to fit the Datagrid
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    }
    // Catch Exception
    catch (Exception ex)
    {
        MessageBox.Show(this, ex.Message, "SQL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

注意:

我知道使用参数,我只是用它来看看它是否会在我稍后添加参数时起作用。

【问题讨论】:

  • Beware of little bobby tables. 很高兴知道您知道使用参数,您还应该知道您不能参数化标识符这一事实。
  • 每个“工作”都有自己的表?
  • 正确,每个作业都有自己的表格。我还有一个主列表,每个插入都与作业表一起放入主表中。所以我需要对每个表进行搜索,并且我将单独搜索主列表。

标签: c# sql search datagridview multiple-columns


【解决方案1】:

这是我用来将所有内容恢复到 DataTable 中的方法

 //'places the call to the system and returns the data as a datatable
    public  DataTable GetDataAsDatatable(List<SqlParameter> sqlParameters, string connStr, string storedProcName)
    {

        var dt = new DataTable();
        var sqlCmd = new SqlCommand();
        using (var sqlconn = new SqlConnection(connStr))
        {
            sqlCmd.Connection = sqlconn;
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = storedProcName;
            sqlCmd.CommandTimeout = 5000;
            foreach (var sqlParam in sqlParameters)
            {
                sqlCmd.Parameters.Add(sqlParam);
            }
            using (var sqlDa = new SqlDataAdapter(sqlCmd))
            {
                sqlDa.Fill(dt);
            }
        }

        sqlParameters.Clear();
        return dt;
    }

    //'places the call to the system and returns the data as a datatable
    public  DataTable GetDataAsDatatable(string connStr, string query)
    {

        var dt = new DataTable();
        var sqlCmd = new SqlCommand();
        using (var sqlconn = new SqlConnection(connStr))
        {
            sqlCmd.Connection = sqlconn;
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.CommandText = query;
            sqlCmd.CommandTimeout = 5000;
            using (var sqlDa = new SqlDataAdapter(sqlCmd))
            {
                sqlDa.Fill(dt);
            }
        }

        return dt;
    }

希望这对你来说是非常自我解释的,但是传入一个 SQL 参数列表、连接字符串和存储过程名称,或者使用你传入内联 SQL 和连接字符串的第二个。

【讨论】:

  • 我明白了,所以您使用的是存储过程。
  • 你可以使用存储过程或内联sql,这里有两种方法。第一个使用存储过程,第二个支持内联 SQL。
  • 所以我也可以使用存储过程来查看数据库中的所有列,并使用它来将其填充回数据网格中......谢谢,你可能刚刚救了我的命。
  • @Mokey 你可以让你的存储过程只返回你想要的列,或者你可以从数据表中删除它们。
【解决方案2】:

我认为您在查询中缺少 '。试试这个...

string selectQuery = "SELECT * FROM dbo.[" + cmbJobName.Text + "] WHERE ([Job Name] LIKE '" +txtSearchJob.Text+ "' OR [Manufacturer] LIKE '" +txtSearchJob.Text+ "')";

【讨论】:

  • 其实,这行得通...我只需要给它添加一个通配符...string selectQuery = "SELECT * FROM dbo.[" + cmbJobName.Text + "] WHERE ([Job Name] LIKE '" +txtSearchJob.Text+ "%' OR [Manufacturer] LIKE '" +txtSearchJob.Text+ "%')";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
相关资源
最近更新 更多