简体   繁体   English

C# 数据网格视图,SQL Server:选择多行,然后将具有指定列的选定行插入到 SQL 表中

[英]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.

相关问题 如何将数据绑定到C#Windows Forms网格视图中的列,这些列存储在SQL表的行中 - How to bind data to the columns in C# Windows Forms grid view, which are stored in rows in SQL table 从 C# 中的 .sql 文件插入 SQL Server 多行 - SQL Server multiple rows insert from .sql file in C# SQL Server对插入行执行操作,该操作会将数据添加到与C#异步的另一个表中 - SQL Server invoke action on insert rows that will add data to another table async from C# 在SQL Server中拆分行和列并插入到临时表中? - Split rows and columns in SQL server and insert into temporary table? 从数据网格视图中选择多行并以逗号分隔的字符串格式添加到变量 C# - Select multiple rows from data grid view and add to a variable in a comma separated string format C# SQL将表2中的表1中的多行与Web表单C#中的其他信息一起插入 - SQL Insert into multiple rows in Table 1 from Table 2 with additional information from web form C# 在插入选择命令sql c#中从文本框中添加行 - add rows from textbox in insert select command sql c# SQL Server 2008中根据多条select语句建表插入行 - Create table and insert rows based on multiple select statements in SQL Server 2008 C# LINQ select from table and join multiple table and join a SQL query view in SQL server - C# LINQ select from table and join multiple table and join a SQL query view in SQL server 如何使用 C# 向 SQL Server 插入/更新行 - How can I insert/update rows with C# to SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM