简体   繁体   English

这是将多行添加到 SQL Server 表的最快方法吗

[英]Is this the fastest way to add multiple rows to a SQL Server table

I have a general question that I have not found any answer to.我有一个一般性的问题,我还没有找到任何答案。 I think it is important to ask this question so I know how to structure the whole application so I am on the right track from the very beginning.我认为提出这个问题很重要,这样我就知道如何构建整个应用程序,这样我从一开始就走在正确的轨道上。

I believe it is practical/efficient to add Multiple rows in one Query command.我相信在一个查询命令中添加多行是实用/高效的。 The below code adds 3 rows with 5 values in 5 columns at once.下面的代码一次在 5 列中添加了 5 个值的 3 行。

See this image also of the table:另请参阅该表的此图像:

数据表的图像

My question now is.我现在的问题是。 This table will later have 100,000 columns where I am thinking of for example adding 10 rows at a time.该表稍后将有 100,000 列,我正在考虑例如一次添加 10 行。 Now imagine that this cmdString will be EXTREMELY long.现在想象这个cmdString会非常长。

My question simply is.我的问题很简单。 Is this okay or is this the wrong way to go here when I add so much information in one query?当我在一个查询中添加如此多的信息时,这是可以的还是这是错误的方式? (Is this the fastest way to do it or should I do the below code in another way?) (这是最快的方法还是我应该以另一种方式执行以下代码?)

Thank you!谢谢!

void addMultipleRowWithValuesSQL()
{
    String cmdString = "INSERT INTO DateTimes(DateTime,F1,F2,G1,G2) " + 
                       "VALUES" +
                           "('201005011715','1',2,'3','4')," +
                           "('201005011730','5',6,'7','8')," +
                           "('201005011745','9',10,'11','12');";
 
    using (SqlConnection conn = new SqlConnection(GetConnectionString()))
    {
        using (SqlCommand comm = new SqlCommand(cmdString))
        {
            try
            {
                comm.Connection = conn;

                conn.Open();
                int i = comm.ExecuteNonQuery();

                if (i != 0) 
                {
                    MessageBox.Show(i + "Rows Added"); 
                }
            }
            catch (SqlException ex) 
            {
                 MessageBox.Show(ex.ToString()); 
            }
        }
    }
}

static private string GetConnectionString()
{
    return "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\andre\\source\\repos\\TestDatabaseCreation\\DatabaseTest.mdf;Integrated Security=True;Connect Timeout=30";
}

They call it Bulk Insert , this link here will get you to a nice example that can explain.他们称之为Bulk Insert ,这里的这个链接会让你看到一个很好的例子,可以解释。 Bulk Insert In SQL Server From C# 从 C# 批量插入 SQL Server

I was looking at this link that also showed of how to add multiple rows in one transaction.我正在查看此链接,该链接还展示了如何在一个事务中添加多行。 https://docs.microsoft.com/en-us/azure/azure-sql/performance-improve-use-batching https://docs.microsoft.com/en-us/azure/azure-sql/performance-improve-use-batching

My question is if this is a valid and good way to do batch insert ?我的问题是,这是否是进行batch insert的有效且好方法?
Or does this code also involve enormous amount of parsing like in my original post, if I would add for example 10,000 rows with VALUES for example 200 columns in one transaction?或者,如果我要在一个事务中添加 10,000 行和 VALUES(例如 200 列),此代码是否也像我原来的帖子一样涉及大量解析?

Just to have a confirmation on that.只是想确认一下。 The code do work to mention.代码确实值得一提。

    void addMultipleRowWithValuesSQL()
    {
        List<string> dbOperations = new List<string>();
        dbOperations.Add("INSERT INTO DateTimes(DateTime, F1, F2, G1, G2) VALUES('201005011800', '11', '22', '33', '44');");
        dbOperations.Add("INSERT INTO DateTimes(DateTime, F1, F2, G1, G2) VALUES('201005011815', '55', '66', '77', '88');");
        dbOperations.Add("INSERT INTO DateTimes(DateTime, F1, F2, G1, G2) VALUES('201005011830', '99', '100', '101', '102');");

        using (SqlConnection conn = new SqlConnection(GetConnectionString()))
        {
            conn.Open();
            SqlTransaction transaction = conn.BeginTransaction();

            foreach (string commandString in dbOperations)
            {
                SqlCommand cmd = new SqlCommand(commandString, conn, transaction);
                cmd.ExecuteNonQuery();
            }
            transaction.Commit();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#在SQL Server上的临时表中插入3万行的最快方法 - Fastest way to insert 30 thousand rows in a temp table on SQL Server with C# 在这种情况下,将数百万行插入SQL表的最快方法是什么? - What is the fastest way to insert millions of rows into a SQL table in this case? 将 dataGridView 行导出到 Excel 或 SQL 服务器数据库的最快方法是什么 - What is the fastest way to export dataGridView rows to Excel or into an SQL Server database 在SQL Server中选择整个表的最快方法是什么? - What is the Fastest Way to Select a Whole Table in SQL Server? C#+ SQL Server-最快/最有效的方式将新行读入内存 - C# + SQL Server - Fastest / Most Efficient way to read new rows into memory 将从文本文件解析的250万行数据插入Sql Server的最快方法是什么 - What would be the fastest way to insert 2.5 million rows of data parsed from a text file, into Sql server 判断SQL Server是否可用的最快方法 - Fastest way to tell if SQL Server is available 将DataTable引入SQL Server的最快方法是什么? - What is the fastest way to get a DataTable into SQL Server? SQL Server参照完整性添加行父表 - SQL Server referential integrity add rows parent table 将ConcurrentQueue中的多行插入到SQL Server表中 - Inserting multiple rows from ConcurrentQueue into SQL Server table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM