大数据插入<转>
Code
//创建一个DataTable
DataTable myTableProInfoT = new DataTable();
myTableProInfoT.Columns.Add("proInfo_product_flag", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_infoFlag", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_status", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_infoDescB", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_operator_noA", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_overTime", Type.GetType("System.String"));
//填充数据
int _iReadNum = 0;
for (_iReadNum = 0; _iReadNum < allCardList.Count; _iReadNum++)
{
DataRow myRowProInfoT = myTableProInfoT.NewRow();
myRowProInfoT["proInfo_product_flag"] = "0001";
myRowProInfoT["proInfo_infoFlag"] = allCardList[_iReadNum].ToString();
myRowProInfoT["proInfo_status"] = "3";
myRowProInfoT["proInfo_infoDescB"] = allBoxList[_iReadNum].ToString();
myRowProInfoT["proInfo_operator_noA"] = StaticClass.operatorNO;
myRowProInfoT["proInfo_overTime"] = sailTime;
myTableProInfoT.Rows.Add(myRowProInfoT);
}
//把DataTable 数据插入到数据库中
using (SqlConnection destinationConnection = new SqlConnection(connectionString))
{
// open the connection
destinationConnection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString))
{
// column mappings
//对应
bulkCopy.ColumnMappings.Add("proInfo_product_flag", "proInfo_product_flag");
bulkCopy.ColumnMappings.Add("proInfo_infoFlag", "proInfo_infoFlag");
bulkCopy.ColumnMappings.Add("proInfo_status", "proInfo_status");
bulkCopy.ColumnMappings.Add("proInfo_infoDescB", "proInfo_infoDescB");
bulkCopy.ColumnMappings.Add("proInfo_operator_noA", "proInfo_operator_noA");
bulkCopy.ColumnMappings.Add("proInfo_overTime", "proInfo_overTime");
//插入的表名
bulkCopy.DestinationTableName = "proInfo_t";
bulkCopy.WriteToServer(myTableProInfoT);
}
}
原文地址:http://www.cnblogs.com/sail/archive/2009/07/21/1527865.html
//创建一个DataTable
DataTable myTableProInfoT = new DataTable();
myTableProInfoT.Columns.Add("proInfo_product_flag", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_infoFlag", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_status", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_infoDescB", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_operator_noA", Type.GetType("System.String"));
myTableProInfoT.Columns.Add("proInfo_overTime", Type.GetType("System.String"));
//填充数据
int _iReadNum = 0;
for (_iReadNum = 0; _iReadNum < allCardList.Count; _iReadNum++)
{
DataRow myRowProInfoT = myTableProInfoT.NewRow();
myRowProInfoT["proInfo_product_flag"] = "0001";
myRowProInfoT["proInfo_infoFlag"] = allCardList[_iReadNum].ToString();
myRowProInfoT["proInfo_status"] = "3";
myRowProInfoT["proInfo_infoDescB"] = allBoxList[_iReadNum].ToString();
myRowProInfoT["proInfo_operator_noA"] = StaticClass.operatorNO;
myRowProInfoT["proInfo_overTime"] = sailTime;
myTableProInfoT.Rows.Add(myRowProInfoT);
}
//把DataTable 数据插入到数据库中
using (SqlConnection destinationConnection = new SqlConnection(connectionString))
{
// open the connection
destinationConnection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString))
{
// column mappings
//对应
bulkCopy.ColumnMappings.Add("proInfo_product_flag", "proInfo_product_flag");
bulkCopy.ColumnMappings.Add("proInfo_infoFlag", "proInfo_infoFlag");
bulkCopy.ColumnMappings.Add("proInfo_status", "proInfo_status");
bulkCopy.ColumnMappings.Add("proInfo_infoDescB", "proInfo_infoDescB");
bulkCopy.ColumnMappings.Add("proInfo_operator_noA", "proInfo_operator_noA");
bulkCopy.ColumnMappings.Add("proInfo_overTime", "proInfo_overTime");
//插入的表名
bulkCopy.DestinationTableName = "proInfo_t";
bulkCopy.WriteToServer(myTableProInfoT);
}
}