如何通过存储过程从数据表中将数据插入到SQL Server中 [英] How to insert data into a sql server from data table via stored procedure

查看:215
本文介绍了如何通过存储过程从数据表中将数据插入到SQL Server中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过存储过程从数据表中将数据插入到sql server中

How to insert data into a sql server from data table via stored procedure

推荐答案

阅读此链接:



http://stackoverflow.com/questions/23166121/how-to-insert-data-into-a-sql-data-table-via-stored-procedure



http://www.codeproject.com/Articles/412802/Sending-a-DataTable-to-a-Stored-Procedure
Read this Link:

http://stackoverflow.com/questions/23166121/how-to-insert-data-into-a-sql-data-table-via-stored-procedure"

"http://www.codeproject.com/Articles/412802/Sending-a-DataTable-to-a-Stored-Procedure"


查看这篇简单的文章



将数据表传递给SQL Server 2008中的存储过程 [ ^ ]
See this simple Article

Passing a datatable to a Stored Procedure in SQL Server 2008[^]


public void  InsertDataTable(DataTable dt)
        {
            Int64 result = 0;
            using (SqlConnection con = new SqlConnection(connection))
            {
                con.Open();
                SqlCommand command = con.CreateCommand();
                SqlTransaction transaction;

                // Start a local transaction.
                transaction = con.BeginTransaction("PortingConnection");

                // Must assign both transaction object and con 
                // to Command object for a pending local transaction
                command.Connection = con;
                command.Transaction = transaction;

                try
                {
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con, SqlBulkCopyOptions.Default, transaction))
                        {
                            bulkCopy.DestinationTableName = "DatabaseTable";
                            bulkCopy.BatchSize = 5000;
                            bulkCopy.WriteToServer(dt);
                            bulkCopy.Close();

                        }

                        transaction.Commit();

                                   

                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
            }



        }


这篇关于如何通过存储过程从数据表中将数据插入到SQL Server中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆