SqlBulkCopy批量添加数据
var sqlconn = ConfigurationManager.ConnectionStrings["SQLConnStringRead"].ConnectionString; string sql = "select * from TicketRecord where 1=2"; //Database db = new Database(sqlconn); //var ds = db.QueryExtend(sql); DataTable ImportDT = SqlHelper.ExecuteDataTable(SqlHelper.SQLConnStringRead, CommandType.Text, sql); for (int i = 1; i <= count; i++) { DataRow dr = ImportDT.NewRow(); var tn = "SK".PadRight(8 - i.ToString().Length, \'0\') + i; dr["TicketNumber"] = tn; dr["SceneryId"] = 0; dr["SceneryName"] = ""; dr["EnterpriseId"] = 0; dr["EnterpriseName"] = ""; dr["BatchNumber"] = ""; dr["SerialId"] = ""; dr["ParentId"] = 0; dr["TicketClassId"] = 0; dr["TicketClassName"] = ""; dr["TicketTypeId"] = 0; dr["TicketTypeName"] = ""; dr["RetailPrice"] = 0; dr["Discount"] = 0; dr["PeopleCount"] = 0; dr["TotalAmount"] = 0; dr["RefundPackageNum"] = 0; dr["RefundNum"] = 0; dr["RefundAmount"] = 0; dr["FreePersonCount"] = 0; dr["ValidStartTime"] = "2015-08-12"; dr["ValidEndTime"] = "2015-08-12"; dr["SaleDate"] = "2015-08-12"; dr["UseNumber"] = 0; dr["HasCheckedNum"] = 0; dr["PayType"] = 0; dr["PayStatus"] = 0; dr["TicketStatus"] = 0; dr["DrawWay"] = 0; dr["PrintType"] = 0; dr["PackageNum"] = 0; dr["HostName"] = ""; dr["RowStatus"] = 0; dr["CreatorId"] = 0; dr["CreateBy"] = ""; dr["CreateOn"] = "2015-08-12"; dr["UpdateId"] = 0; dr["UpdateBy"] = ""; dr["UpdateOn"] = "2015-08-12"; dr["Prefix"] = ""; dr["Postfix"] = ""; dr["MidNum"] = ""; dr["Remark"] = ""; dr["BookMobile"] = ""; dr["ActivateDate"] = "2015-08-12"; dr["InsidePrice"] = 0; ImportDT.Rows.Add(dr); } SqlConnectionStringBuilder sqlcsb = new SqlConnectionStringBuilder(); sqlcsb.ConnectionString = ConfigurationManager.ConnectionStrings["SQLConnStringRead"].ConnectionString; SqlConnection SqlConn = new SqlConnection(sqlcsb.ConnectionString); SqlConn.Open(); SqlBulkCopy bulkCopy = new SqlBulkCopy(SqlConn); ; bulkCopy.BatchSize = count; bulkCopy.BulkCopyTimeout = 500000000; bulkCopy.DestinationTableName = "TicketRecord"; bulkCopy.WriteToServer(ImportDT); bulkCopy.Close(); SqlConn.Close();
版权声明:本文为jiangqw原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。