PetaPoco批量插入数据

  

  VS添加完组件,自动生成的PetaPoco.cs文件中没有SqlBulkInsert这个方法,但是可以在里面添加,代码如下:

        /// <summary>
        /// BulkInsert
        /// </summary>
        /// <param name="dt">dttable(must 1、columns exactly alike,2、without self-increasing primary key)</param>
        /// <param name="tableName">table name(must include schema)</param>
        /// <param name="fieldName">field name string[](must 1、exactly alike,2、without self-increasing primary key)</param>
        /// <returns></returns>
        public bool SqlBulkInsert(DataTable dt, string tableName, string[] fieldName)
        {
            try
            {
                OpenSharedConnection();
                var entry = ConfigurationManager.ConnectionStrings[1];//获取web.config中的数据库连接字符串
                using (SqlBulkCopy bulk = new SqlBulkCopy(entry.ConnectionString))
                {
                    try
                    {
                        bulk.DestinationTableName = tableName;
                        foreach (string field in fieldName)
                        {
                            bulk.ColumnMappings.Add(field, field);
                        }
                        bulk.WriteToServer(dt);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                    finally
                    {
                        bulk.Close();
                    }
                }
            }
            finally
            {
                CloseSharedConnection();
            }
        }

 

  第一个参数不解释了,把DataTable传过来;第二个是表名,需要注意的是这边需要SQL Server表的全名,包括schema。一般是dbo,但也有可能是别的,例如:org.Provinces;第三个是DataTable中的列名数组,需要注意的是大小写要和数据库中一致。

 

  如果是List类型数据源,可以通过以下方法得到DataTable数据和其列名数组:

public class ModelConvertHelper<T> where T : new()
    {
//List<T>转成DataTable并返回列名数组
 public static Tuple<DataTable, string[]> batchExecData(List<T> List)
        {
            DataTable dt = new DataTable();
            List<string> list = new List<string>();
            foreach (T entity in List)
            {
                DataRow dr = dt.NewRow();
                foreach (PropertyInfo column in entity.GetType().GetProperties())
                {
                    if (!dt.Columns.Contains(column.Name))
                    {
                        dt.Columns.Add(column.Name);
                        list.Add(column.Name);
                    }
                    object value = column.GetValue(entity);
                    if (value != null)
                    {
                        dr[column.Name] = value;
                    }
                }
                dt.Rows.Add(dr);
            }
            return new Tuple<DataTable, string[]>(dt, list.ToArray());
        }
}

 

  调用方法:

//得到list数据
Tuple<DataTable, string[]> tule = ChineseAbs.DealLab.Helpers.ModelConvertHelper<AssetPoolData>.batchExecData(list);
var res = m_db.SqlBulkInsert(tule.Item1, "org.Provinces", tule.Item2);

 

  效果还不错,测试了两万条数据,用了0.5秒左右。

 

By QJL

posted @ 2017-10-25 14:39  风一样的大叔  阅读(842)  评论(0编辑  收藏  举报