zoukankan      html  css  js  c++  java
  • 批量导入数据库

        public bool MsSqlBulkCopyData(DataTable dt, string connectionString)
            {
                bool result;
                try
                {
                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        conn.Open();
                        SqlTransaction trans = conn.BeginTransaction();
                        SqlBulkCopy sqlbulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, trans);
                        sqlbulkCopy.DestinationTableName = dt.TableName;
                        sqlbulkCopy.BulkCopyTimeout = 1000;
                        foreach (DataColumn dtColumn in dt.Columns)
                        {
                            sqlbulkCopy.ColumnMappings.Add(dtColumn.ColumnName, dtColumn.ColumnName);
                        }
                        try
                        {
                            sqlbulkCopy.WriteToServer(dt);
                            trans.Commit();
                            result = true;
                        }
                        catch
                        {
                            trans.Rollback();
                            sqlbulkCopy.Close();
                            result = false;
                        }
                        finally
                        {
                            sqlbulkCopy.Close();
                            conn.Close();
                        }
                    }
                }
                catch (Exception e)
                {
                    this.Logger.WriteLog("-----------利用Net SqlBulkCopyData 批量导入数据库,速度超快-----------
    " + e.Message + "
    ");
                    result = false;
                }
                return result;
            }
  • 相关阅读:
    小D课堂
    小D课堂
    小D课堂
    小D课堂
    小D课堂
    c++ , const对象中的变量不能被修改
    C++,常成员函数
    c++,给常成员变量赋值
    C++中,引用作为函数参数
    C++中,如何定义和使用指向成员函数的指针
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/6846048.html
Copyright © 2011-2022 走看看