具体代码如下:
/// <summary>
/// 批量插入数据到BayonetZipFailedPic表
/// </summary>
/// <param name="bayonetFailedPicList">含有多条拷贝失败的二次卡口图片数据的集合</param>
/// <returns>0-成功,-2-异常,其他-失败</returns>
public int BatchAddBayonetZipFailedPic(List<BayonetZipFailedPic> bayonetFailedPicList)
{
int result = ;
try
{
DataTable dataTable = GetBayonetZipFailedPicTableSchema();
foreach (BayonetZipFailedPic bayonetFailedPic in bayonetFailedPicList)
{
DataRow dataRow = dataTable.NewRow();
dataRow[] = bayonetFailedPic.ZipFileID;
dataRow[] = bayonetFailedPic.FileOriName;
dataRow[] = bayonetFailedPic.FileFullPath;
dataRow[] = System.DateTime.Now;//bayonetPic.OperateTime; dataTable.Rows.Add(dataRow);
}
return BatchAddBayonetZipFailedPic(dataTable, "BayonetZipFailedPic");
}
catch (Exception exception)
{
logger.Error("批量插入BayonetZipFailedPic数据异常!", exception);
result = -;
}
return result;
} /// <summary>
/// 创建和BayonetPic表对应的DataTable对象
/// </summary>
/// <returns>DataTable对象</returns>
private static DataTable GetBayonetPicTableSchema()
{
DataTable dataTable = new DataTable();
dataTable.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("FolderID",typeof(System.Data.SqlTypes.SqlGuid)), new DataColumn("FileOriName"),
new DataColumn("FileName"), new DataColumn("FileFullPath"), new DataColumn("Longitude"),new DataColumn("Latitude"),new DataColumn("Address"),new DataColumn("Contacts"), new DataColumn("ContactWay"), new DataColumn("PicStartTime"),
new DataColumn("PicEndTime"), new DataColumn("OperateTime"), new DataColumn("Status"),new DataColumn("Width"),new DataColumn("Height")}); return dataTable;
} /// <summary>
/// 批量插入数据到BayonetPic表
/// </summary>
/// <param name="bayonetPicList">含有多条二次卡口图片数据的集合</param>
/// <returns>0-成功,-2-异常,其他-失败</returns>
public int BatchAddBayonetPic(List<BayonetPic> bayonetPicList)
{
int result = ;
try
{
DataTable dataTable = GetBayonetPicTableSchema();
foreach (BayonetPic bayonetPic in bayonetPicList)
{
DataRow dataRow = dataTable.NewRow();
dataRow[] = new Guid(bayonetPic.FolderID);
dataRow[] = bayonetPic.FileOriName;
dataRow[] = bayonetPic.FileName;
dataRow[] = bayonetPic.FileFullPath;
dataRow[] = bayonetPic.Longitude;
dataRow[] = bayonetPic.Latitude;
dataRow[] = bayonetPic.Address;
dataRow[] = bayonetPic.Contacts;
dataRow[] = bayonetPic.ContactWay;
dataRow[] = bayonetPic.PicStartTime;
dataRow[] = bayonetPic.PicEndTime;
dataRow[] = System.DateTime.Now;//bayonetPic.OperateTime;
dataRow[] = bayonetPic.Status;
dataRow[] = bayonetPic.Width;
dataRow[] = bayonetPic.Height;
dataTable.Rows.Add(dataRow);
}
return BatchAddBayonetPic(dataTable, "BayonetPic");
}
catch (Exception exception)
{
logger.Error("批量插入BayonetPic数据异常!", exception);
result = -;
}
return result;
} private int BatchAddBayonetPic(DataTable dt, string tableName)
{
int result = ;
DBManager dbManager = this.dbConnector.GetDbManager(ConUtil.CaseId);
if (dbManager == null)
{
logger.Error("数据库连接未建立!");
result = -;
}
else
{
dbManager.SqlBulkCopyInsert(this.ConnectionString, this.DbType, dt, tableName);
result = ;
}
return result;
} public static int SqlBulkCopyInsert(string connectionString, ProviderType providerType, DataTable dataTable, string tableName)
{
if (connectionString == null || connectionString.Length == )
{
throw new ArgumentNullException("connectionString is null or empty!");
}
int result = ;
using (DbConnection dbConnection = DbFactory.GetProvider(providerType).CreateConnection())
{
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connectionString);
sqlBulkCopy.DestinationTableName = tableName;
sqlBulkCopy.BatchSize = dataTable.Rows.Count;
dbConnection.ConnectionString = connectionString;
dbConnection.Open();
if (dataTable != null && dataTable.Rows.Count != )
{
sqlBulkCopy.WriteToServer(dataTable);
}
sqlBulkCopy.Close();
dbConnection.Close();
}
return result;
}
注意:以上代码不能直接拷贝执行,只是用法示例