.net使用SqlBulkCopy导入数据(创建新表)

原文:.net使用SqlBulkCopy导入数据(创建新表)

.net2.0后ado.net提供了一个快速导入sqlserver的方法sqlbulkcopy.导入效率非常高. 

包装了一个简单的sqlbulkcopy类,用于数据从datatable导入到sqlserver.代码如下: 
 

    /// <summary>
        /// 将DataTable写入数据库的表中
        /// </summary>
        /// <param name="source">数据源DataTable</param>
        /// <param name="tableName">数据目标的表名</param>
        /// <param name="useTransaction">操作过程是否使用事务</param>
        /// <param name="databaseConnString">数据库连接字符串</param>
        /// <param name="dropTable">删除DB中已存在的表(并自动新建表)</param>
        /// <param name="primaryKeys">主键的列名</param>
        public void WriteToDataBase(DataTable source, string tableName, bool useTransaction, string databaseConnString, bool dropTable, string[] primaryKeys)
        {
            //判断表是否存在 
            SqlHelper.ConnectionString = databaseConnString;
            //dataHelper.IsConnString = true; //使用数据库连接字符串创建sqlserver操作对象 
            string sql = "select * from sys.objects where type=‘U‘ and name=‘" + tableName + "";
            DataTable dt = SqlHelper.ExecuteDataset(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                if (dropTable == true)
                {
                    sql = "drop table " + tableName + "";   //清除已存在的表
                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, sql);
                }
                else
                {
                    SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(databaseConnString, SqlBulkCopyOptions.UseInternalTransaction | SqlBulkCopyOptions.FireTriggers);
                    sqlbulkcopy.DestinationTableName = tableName;//数据库中的表名

                    sqlbulkcopy.WriteToServer(source);
                    return;
                }
            }
            this.CreateTable(source.Columns, tableName, primaryKeys);
            var sqlBulkCopy = new System.Data.SqlClient.SqlBulkCopy(databaseConnString, SqlBulkCopyOptions.FireTriggers);//启动触发器
            if (useTransaction == true)
            {
                sqlBulkCopy = new System.Data.SqlClient.SqlBulkCopy(databaseConnString, SqlBulkCopyOptions.UseInternalTransaction | SqlBulkCopyOptions.FireTriggers); //导入的数据在一个事务中 
            }
            sqlBulkCopy.DestinationTableName = tableName;
            foreach (DataColumn c in source.Columns)
            {
                sqlBulkCopy.ColumnMappings.Add(c.ColumnName, c.ColumnName);
            }
            //SqlBulkCopy.BulkCopyTimeout = this.timeout;  //超时时间 
            sqlBulkCopy.BatchSize = 3000;  //每次传输3000行 
            sqlBulkCopy.WriteToServer(source);
        }

 

        /// <summary>
        /// 创建表以及触发器
        /// </summary>
        /// <param name="columns">列名</param>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKeys">主键的列名</param>
        public void CreateTable(System.Data.DataColumnCollection columns, string tableName, string[] primaryKeys)
        {
            if (primaryKeys == null || primaryKeys.Length < 1)
            {
                MessageBox.Show("主键不允许为空!");
                return;
            }
            StringBuilder sb = new StringBuilder();
            // sb.Append("create table [" + tableName + "] (autoId  int identity(1,1),");
            sb.Append("create table [" + tableName + "] (");
            foreach (DataColumn column in columns)
            {
                sb.Append(" [" + column.ColumnName + "] " + this.GetTableColumnType(column.DataType) + ",");
            }

            string sql = sb.ToString();
            sql = sql.TrimEnd(,);
            sql += ")";

            sb.Clear();
            var temp1 = primaryKeys;
            for (int i = 0; i < primaryKeys.Length; i++)
            {
                temp1[i] = tableName + "." + primaryKeys[i] + "=Inserted." + primaryKeys[i];
            }
            List<string> temp2 = new List<string>();
            for (int i = 0; i < columns.Count; i++)
            {
                temp2.Add(columns[i].ColumnName + "=Inserted." + columns[i].ColumnName);
            }
            List<string> temp3 = new List<string>();
            for (int i = 0; i < columns.Count; i++)
            {
                temp3.Add(columns[i].ColumnName);
            }
            sb.Append("CREATE TRIGGER [tri_" + tableName + "_edit]  ON [" + tableName + "] instead of insert as");
            sb.Append("  IF EXISTS (");
            sb.Append("SELECT * FROM    " + tableName + ",Inserted WHERE " + String.Join(" AND ", temp1) + ")");
            sb.Append(" UPDATE [" + tableName + "] SET " + string.Join(",", temp2) + " FROM [" + tableName + "] JOIN inserted ON " + String.Join(" AND ", temp1) + " ");
            sb.Append(" ELSE ");

            sb.Append(" INSERT  [" + tableName + "](" + string.Join(",", temp3) + ") SELECT  " + string.Join(",", temp3) + " FROM inserted  ");
           // sql = sql + " ; " + sb.ToString(); 
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, sql);
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, sb.ToString());
        }

 

       private string GetTableColumnType(System.Type type)
        {
            string result = "varchar(255)";
            string sDbType = type.ToString();
            switch (sDbType)
            {
                case "System.String":
                    break;
                case "System.Int16":
                    result = "int";
                    break;
                case "System.Int32":
                    result = "int";
                    break;
                case "System.Int64":
                    result = "float";
                    break;
                case "System.Decimal":
                    result = "decimal(18,4)";
                    break;
                case "System.Double":
                    result = "decimal(18,4)";
                    break;
                case "System.DateTime":
                    result = "datetime";
                    break;
                default:
                    break;
            }
            return result;
        }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。