Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将c datagridview选定的单元格值传递给SQL查询_C#_Sql Server_Datagridview - Fatal编程技术网

C# 如何将c datagridview选定的单元格值传递给SQL查询

C# 如何将c datagridview选定的单元格值传递给SQL查询,c#,sql-server,datagridview,C#,Sql Server,Datagridview,我有一个C窗体上的datagridview,它有大量的行。和带有复选框的选择选项 如果使用复选框选择选择10行,则需要将列select row值传递给SQL查询,以筛选来自其他表的记录 下面是我的代码,但它不工作,我不知道如何解决这个问题 private void button1_Click(object sender, EventArgs e) { SqlConnection conn1 = new SqlConnection(@"Data Source=.\sqlexpress;I

我有一个C窗体上的datagridview,它有大量的行。和带有复选框的选择选项

如果使用复选框选择选择10行,则需要将列select row值传递给SQL查询,以筛选来自其他表的记录

下面是我的代码,但它不工作,我不知道如何解决这个问题

private void button1_Click(object sender, EventArgs e)
{
     SqlConnection conn1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=acc;Integrated Security=True");
     SqlCommand cmd1 = new SqlCommand(@"select db.date,db.type,db.refno,db.itmcod,db.qty,db.cuscod, db.cstcod,cus.cusnam INTO ##wec from fstktxn as db INNER JOIN fcustomer as cus on db.cuscod = cus.cuscod where itmcod = "dataGridView1.Rows[j].Cells["title"].Value", ", conn1);

     conn1.Open();
     cmd1.ExecuteNonQuery();

     SqlBulkCopy bulkCopy = new SqlBulkCopy(conn1);
     bulkCopy.DestinationTableName = "##tmp1";

     conn1.Close();
}

是否有任何解决方案可以使用gridview选择过滤数据?

如果您试图获取选中的复选框行,则可以使用此选项获取它

 protected void button1_Click(object sender, EventArgs e)  
    {  
        foreach (GridViewRow gvrow in dataGridView1.Rows)  
        {  
            CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");  
            if (chk != null & chk.Checked)  
            {  
                SqlConnection conn1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=acc;Integrated Security=True");
                SqlCommand cmd1 = new SqlCommand(@"select db.date,db.type,db.refno,db.itmcod,db.qty,db.cuscod, db.cstcod,cus.cusnam INTO ##wec from fstktxn as db INNER JOIN fcustomer as cus on db.cuscod = cus.cuscod where itmcod = "dataGridView1.Cells["title"].Value", ", conn1);
                conn1.Open();
                cmd1.ExecuteNonQuery();
               SqlBulkCopy bulkCopy = new SqlBulkCopy(conn1);
               bulkCopy.DestinationTableName = "##tmp1";
               conn1.Close();//Do something 
            }  
        }         
    }  

如果我知道你是需求,你应该这样做

首先获取DataGridVew数据

DataTable data=datfridviewname.DataSource as DataTable;
                con.Open();
                SqlBulkCopy sbc = new SqlBulkCopy(con);
                sbc.DestinationTableName = @"tableName";
                sbc.WriteToServer(data);
试着读一下这个

它显示了如何将变量安全地添加到查询中

我想您要问的是如何使用DataGridView中选定的行来形成in查询?。假设这是try,您应该将其分为两个步骤:

从UI获取应在in查询中使用的值集 在查询中组合并创建 第一步可以通过迭代DataGridView的.Rows并检查您拥有的DataGridViewCheckBoxColumn的.Checked值来完成;如果选中:将筛选值添加到某个列表中。例如:

List<string> titles = new List<string>();
foreach (GridViewRow row in dataGridView1.Rows)  
{  
    if(row.Cells["CheckBoxColumnName"].Value == true)
    {
        titles.Add((string)row.Cells["TitleColumnName"].Value);
    }
}
var sb = new StringBuilder("...the basic query... where someColumn in (");
int index = 0;
var cmd = new SqlCommand(connection); // see also: "using"
foreach(var title in titles) {
    if(index != 0) sb.Append(",");
    string name = "@title" + index++;
    sb.Append(name);
    cmd.Parameters.AddWithValue(name, value);
}
sb.Append(")");
cmd.CommandText = sb.ToString();

然后像往常一样执行命令。您可能还需要考虑零的情况;这应该是所有行吗?没有行?

您在这里想做什么?您有一个正在使用ExecuteOnQuery执行的查询cmd,因此它什么也不做。您有一个从未调用WriteToServer的SqlBulkCopy,事实上,您在问题中没有提到任何有关BulkCopy的内容。你想干什么?您提到了一些UI元素,但在代码中根本没有涉及它们。这个问题非常不清楚。需要使用datagridview selectionfilter从sql表中筛选数据吗?代码中的j是什么?预期的结果是什么?这意味着什么?你是在问我怎样才能把这变成一个查询?您正在尝试筛选屏幕上的数据吗?或者获取的数据?哦,还有:永远不要将输入连接到查询中;这是一个巨大的SQL注入问题。如何将这些选定的行值插入sql查询我不认为它们实际上是在尝试执行SQLBulkCopy。这没关系,但问题是在sql查询where子句中使用gridview选定的行来筛选临时表中的数据。我需要知道怎么做that@RaziAngel你的问题没有问这个,这个答案也没有回答这个问题;所以
var sb = new StringBuilder("...the basic query... where someColumn in (");
int index = 0;
var cmd = new SqlCommand(connection); // see also: "using"
foreach(var title in titles) {
    if(index != 0) sb.Append(",");
    string name = "@title" + index++;
    sb.Append(name);
    cmd.Parameters.AddWithValue(name, value);
}
sb.Append(")");
cmd.CommandText = sb.ToString();