C#批量入库

public static void BulkCopyToDB(DataTable dt, string conn, string tableName, out string msg)
        {
            msg = "";

            if (dt.Rows.Count > 0)
            {
                using (SqlConnection connection = new SqlConnection(conn))
                {
                    connection.Open();

                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
                    {
                        bulkCopy.DestinationTableName = tableName;
                        try
                        {
                            bulkCopy.WriteToServer(dt);
                        }
                        catch (Exception ex)
                        {
                            msg = ex.Message.ToString();
                            BLL.Loger.Log4Net.Error(ex.Message, ex);
                        }
                        finally
                        {
                            bulkCopy.Close();
                        }
                    }
                }
            }
        }

 

posted @ 2015-11-27 14:58  stevejson  阅读(546)  评论(0编辑  收藏  举报