[英]C# Data grid view, SQL Server: Select multiple Rows then insert selected rows with specified columns into SQL table
In my current application I have a snippet of code that allows me to select a single row in a data grid view and store all the columns information into a variable to do with what I want.在我当前的应用程序中,我有一段代码,它允许我在数据网格视图中选择一行并将所有列信息存储到一个变量中,以完成我想要的操作。 I use this primarily to send information from one SQL database table to another.
我主要使用它来将信息从一个 SQL 数据库表发送到另一个。 It's great for only sending specified cells within a row.
它非常适合仅在一行中发送指定的单元格。
Here is how I do a single row:这是我如何做单行:
string ID = dataGridView1.SelectedRows[0].Cells[0].Value + string.Empty;
string itemOne= dataGridView1.SelectedRows[0].Cells[1].Value + string.Empty;
string itemTwo= dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty;
string itemThree= dataGridView1.SelectedRows[0].Cells[3].Value + string.Empty;
var vItemOne = itemOne;
var vItemTwo= itemTwo;
var vItemThree= itemThree;
// ETC..
However, I now want to be able to select Multiple Rows and only insert specified columns within those rows to a SQL database.但是,我现在希望能够选择多行并仅将这些行中的指定列插入到 SQL 数据库中。
I've tried modifying the above code to work... obviously it doesn't work.我试过修改上面的代码来工作......显然它不起作用。
I believe I need a loop, I haven't really used loops much so I'm not sure how to make it loop, skip certain columns, then insert into database.我相信我需要一个循环,我还没有真正使用过循环,所以我不确定如何让它循环,跳过某些列,然后插入到数据库中。
This is what I am currently attempting, however I seem to be messing up somewhere.这是我目前正在尝试的,但是我似乎在某个地方搞砸了。
using (SqlConnection con = new SqlConnection(Connection.MTRDataBaseConn))
{
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO dbo.[" + txtJobName.Text + "] ([Item One], [Item Two], [Item Three]) VALUES(@ItemOne,@ItemTwo,@ItemThree)";
cmd.Connection = con;
string strItemOne = this.dataGridView1.SelectedRows[i].Cells[1].Value + string.Empty;
string strItemTwo = this.dataGridView1.SelectedRows[i].Cells[2].Value + string.Empty;
string strItemThree = this.dataGridView1.SelectedRows[i].Cells[3].Value + string.Empty;
//Parameters
cmd.Parameters.AddWithValue("@ItemOne", strItemOne);
cmd.Parameters.AddWithValue("@ItemTwo", strItemTwo);
cmd.Parameters.AddWithValue("@ItemThree", strItemThree);
//execute
cmd.ExecuteNonQuery();
//close connection
con.Close();
}
}
... ...
While Debugging My dataGridView.SelectedRows.Count; i++
在调试我的
dataGridView.SelectedRows.Count; i++
dataGridView.SelectedRows.Count; i++
doesn't seem to be increasing and is staying at 0... I'm receiving the error when I try to return the selected row to a string. dataGridView.SelectedRows.Count; i++
似乎没有增加并保持在 0...当我尝试将所选行返回到字符串时收到错误。 Shouldn't my selected rows still return a value?我选择的行不应该仍然返回一个值吗?
I'm under the assumption my loop is wrong.我假设我的循环是错误的。
Can anyone help me with my issue?任何人都可以帮助我解决我的问题吗?
Simply have to use a for each statement只需为每个语句使用一个
string itemOne= dataGridView1.SelectedRows[0].Cells[1].Value + string.Empty;
string itemTwo= dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty;
string itemThree= dataGridView1.SelectedRows[0].Cells[3].Value + string.Empty;
var vItemOne = itemOne;
var vItemTwo= itemTwo;
var vItemThree= itemThree;
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
//Insert Query Here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.