Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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
Sql server 在sql server 2012上插入15000条记录的最佳方法?_Sql Server_Bulkinsert - Fatal编程技术网

Sql server 在sql server 2012上插入15000条记录的最佳方法?

Sql server 在sql server 2012上插入15000条记录的最佳方法?,sql-server,bulkinsert,Sql Server,Bulkinsert,在sql server 2012中插入15000条或更多记录的最佳方法是什么?存储程序?纯插入?顺便说一下,我正在C#中使用一个.NET应用程序。最好的方法是使用(TVP)作为SQL Server存储过程的参数批量传递数据 第二个最好的方法是在.NET中使用,它也可以接受DataTable作为参数。最好的方法是使用(TVP)批量传递数据作为SQL Server存储过程的参数 其次是在.NET中使用,它也可以接受数据表作为参数。批量复制将是一个不错的选择 批量复制将是一个伟大的选择 using

在sql server 2012中插入15000条或更多记录的最佳方法是什么?存储程序?纯插入?顺便说一下,我正在C#中使用一个.NET应用程序。

最好的方法是使用(TVP)作为SQL Server存储过程的参数批量传递数据


第二个最好的方法是在.NET中使用,它也可以接受DataTable作为参数。

最好的方法是使用(TVP)批量传递数据作为SQL Server存储过程的参数


其次是在.NET中使用,它也可以接受数据表作为参数。

批量复制将是一个不错的选择


批量复制将是一个伟大的选择

using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = GetConnectionString();
        // Open a sourceConnection to the AdventureWorks database. 
        using (SqlConnection sourceConnection =
                   new SqlConnection(connectionString))
        {
            sourceConnection.Open();

            // Perform an initial count on the destination table.
            SqlCommand commandRowCount = new SqlCommand(
                "SELECT COUNT(*) FROM " +
                "dbo.BulkCopyDemoMatchingColumns;",
                sourceConnection);
            long countStart = System.Convert.ToInt32(
                commandRowCount.ExecuteScalar());
            Console.WriteLine("Starting row count = {0}", countStart);

            // Get data from the source table as a SqlDataReader.
            SqlCommand commandSourceData = new SqlCommand(
                "SELECT ProductID, Name, " +
                "ProductNumber " +
                "FROM Production.Product;", sourceConnection);
            SqlDataReader reader =
                commandSourceData.ExecuteReader();

            // Open the destination connection. In the real world you would  
            // not use SqlBulkCopy to move data from one table to the other  
            // in the same database. This is for demonstration purposes only. 
            using (SqlConnection destinationConnection =
                       new SqlConnection(connectionString))
            {
                destinationConnection.Open();

                // Set up the bulk copy object.  
                // Note that the column positions in the source 
                // data reader match the column positions in  
                // the destination table so there is no need to 
                // map columns. 
                using (SqlBulkCopy bulkCopy =
                           new SqlBulkCopy(destinationConnection))
                {
                    bulkCopy.DestinationTableName =
                        "dbo.BulkCopyDemoMatchingColumns";

                    try
                    {
                        // Write from the source to the destination.
                        bulkCopy.WriteToServer(reader);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        // Close the SqlDataReader. The SqlBulkCopy 
                        // object is automatically closed at the end 
                        // of the using block.
                        reader.Close();
                    }
                }

                // Perform a final count on the destination  
                // table to see how many rows were added. 
                long countEnd = System.Convert.ToInt32(
                    commandRowCount.ExecuteScalar());
                Console.WriteLine("Ending row count = {0}", countEnd);
                Console.WriteLine("{0} rows were added.", countEnd - countStart);
                Console.WriteLine("Press Enter to finish.");
                Console.ReadLine();
            }
        }
    }

    private static string GetConnectionString()
        // To avoid storing the sourceConnection string in your code,  
        // you can retrieve it from a configuration file. 
    {
        return "Data Source=(local); " +
            " Integrated Security=true;" +
            "Initial Catalog=AdventureWorks;";
    }
}