如何在 where 子句上基于 SqlCommand 或 SqlDataAdapter 填充 datagridview? SqlDataAdapter 不适用于哪里?

Posted

技术标签:

【中文标题】如何在 where 子句上基于 SqlCommand 或 SqlDataAdapter 填充 datagridview? SqlDataAdapter 不适用于哪里?【英文标题】:How to populate datagridview based on SqlCommand or SqlDataAdapter on where clause? SqlDataAdapter doesn't work with where? 【发布时间】:2021-05-20 21:48:58 【问题描述】:
// for delete it works, but for search no

// this is the delete part, afterwards I call the displaymethod to display the results after deletion
private void button1_Click(object sender, EventArgs e)

    if (txtArtistDelete.Text != "")
    
        command = new SqlCommand("delete from dbo.catalog where artist=@artist", connection);

        connection.Open();
        command.Parameters.AddWithValue("@artist", txtArtistDelete.Text);
        command.ExecuteNonQuery();
        connection.Close();

        MessageBox.Show("Record deleted successfully!");

        DisplayData();
        ClearData();
    
    else
    
        MessageBox.Show("Please select an artist to delete");
    


private void DisplayData()

    connection.Open();

    DataTable dt = new DataTable();
    adpter = new SqlDataAdapter("select * from dbo.catalog", connection);
    adpter.Fill(dt);

    dgvCatalog.DataSource = dt;
    connection.Close();


// but after search I can not do that
private void btnCautare_Click(object sender, EventArgs e)

    command = new SqlCommand("select * from dbo.catalog where title = @title", connection);

    connection.Open();
    command.Parameters.AddWithValue("@title", txtArtistDelete.Text);

    DataTable dt = new DataTable();
    adpter = new SqlDataAdapter("select * from dbo.catalog where title = @title", connection);
    adpter.Fill(dt);

    dgvCatalog.DataSource = dt;

    command.ExecuteNonQuery();
    connection.Close();


// I get this error: 
// System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@title".'  
// I want to see in the datagridview only that searched record. 
// How can I do that with SqlCommand and SqlDataAdapter?     

如何使用 where 子句基于 SqlCommandSqlDataAdapter 填充 datagridview? SqlDataAdapter 不适用于 where

【问题讨论】:

dt.Fill(reader),?您可以从command.ExecuteReader() 获取读者。不要忘记使用 using 块处理所有 SQL 对象 注意:有很多 更有效的方法可以使用这些数据库提供程序对象,并且所有这些都有很好的文档记录。对于“搜索”,您可以使用 DGV 数据源的 RowFilter 属性过滤现有结果。 【参考方案1】:

您可以更高效地执行此操作,但让我们继续使用上面的代码。 正如错误所说“必须声明标量变量“@title”。”

用这个替换你的代码

command = new SqlCommand("select *  from dbo.catalog where title=@title", connection);
connection.Open();
command.Parameters.AddWithValue("@title", txtArtistDelete.Text);

DataTable dt = new DataTable();
adpter = new SqlDataAdapter(command); //Pass the SqlCommand object command as argument to SqlDataAdapter as it already contains the parameter @title in it
adpter.Fill(dt);
dgvCatalog.DataSource = dt;

command.ExecuteNonQuery();
connection.Close();

【讨论】:

以上是关于如何在 where 子句上基于 SqlCommand 或 SqlDataAdapter 填充 datagridview? SqlDataAdapter 不适用于哪里?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 mysqli 准备语句中使用多个内部连接和多个 WHERE 子句? [复制]

如果 where 子句已经修复,如何加快 spark sql 过滤器查询?

如何通过文本框过滤数据表视图中的子表单? #likeoperator #where 子句

Where 子句基于优先级

JPA标准WHERE子句

如何在从 .Include 实体框架查询返回的数据上指定 where 子句?