问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Diagnostics;usingSystem.Data.SqlClient;usingSystem.Data;namespace样本采购订单{classProgram{staticvoidMain(string[]args){Stopwatchst=newStopwatch();st.Start();stringconnectionString=@"DataSource=192.168.0.20;InitialCatalog=misdb;UserID=sa;Password=123456;Replication=True";stringSqlConnectionStringTarget="DataSource=192.168.0.20;InitialCatalog=Basedb;UserID=sa;Password=123456;Replication=True";//DataTablesourceData=newDataTable();//源using(SqlConnectionsourceConnection=newSqlConnection(connectionString)){//源表查询数据SqlCommandmyCommand=newSqlCommand("select*fromReceDetail",sourceConnection);sourceConnection.Open();SqlDataReaderreader=myCommand.ExecuteReader(CommandBehavior.CloseConnection);//sourceData.Load(reader);//sourceConnection.Close();//目的/**这里用的是同一个连接,**如果不是同一个数据库,则用不同的连接**注意:一定要按照我的连接字符串的格式来写,不然是不行的***/using(SqlConnectionTarget=newSqlConnection(SqlConnectionStringTarget)){//打开连接//Target.Open();using(SqlBulkCopybulkCopy=newSqlBulkCopy(Target.ConnectionString,SqlBulkCopyOptions.UseInternalTransaction)){//源列字段:A,目标数据源字段:BSqlBulkCopyColumnMappingm1=newSqlBulkCopyColumnMapping("ShopCode","ShopName");SqlBulkCopyColumnMappingm2=newSqlBulkCopyColumnMapping("ReceNo","ReceNo");SqlBulkCopyColumnMappingm4=newSqlBulkCopyColumnMapping("TranDate","TranDate");SqlBulkCopyColumnMappingm6=newSqlBulkCopyColumnMapping("RealAmount","RealAmount");SqlBulkCopyColumnMappingm7=newSqlBulkCopyColumnMapping("DiscountType","DiscountType");SqlBulkCopyColumnMappingm8=newSqlBulkCopyColumnMapping("Discount","Discount");SqlBulkCopyColumnMappingm9=newSqlBulkCopyColumnMapping("ReceType","ReceType");SqlBulkCopyColumnMappingm10=newSqlBulkCopyColumnMapping("AccountDiff","AccountName");SqlBulkCopyColumnMappingm12=newSqlBulkCopyColumnMapping("CheckNo","CheckNo");SqlBulkCopyColumnMappingm13=newSqlBulkCopyColumnMapping("Comment","Comment");SqlBulkCopyColumnMappingm16=newSqlBulkCopyColumnMapping("Operator","CreatePerson");SqlBulkCopyColumnMappingm17=newSqlBulkCopyColumnMapping("EnableFlag","DeleteFlag");SqlBulkCopyColumnMappingm18=newSqlBulkCopyColumnMapping("CreateDate","CreateTime");bulkCopy.ColumnMappings.Add(m1);bulkCopy.ColumnMappings.Add(m2);bulkCopy.ColumnMappings.Add(m4);bulkCopy.ColumnMappings.Add(m6);bulkCopy.ColumnMappings.Add(m7);bulkCopy.ColumnMappings.Add(m8);bulkCopy.ColumnMappings.Add(m9);bulkCopy.ColumnMappings.Add(m10);bulkCopy.ColumnMappings.Add(m12);bulkCopy.ColumnMappings.Add(m13);bulkCopy.ColumnMappings.Add(m16);bulkCopy.ColumnMappings.Add(m17);bulkCopy.ColumnMappings.Add(m18);//有多列时类推:/***SqlBulkCopyColumnMappingm1=newSqlBulkCopyColumnMapping("A","B");*bulkCopy.ColumnMappings.Add(m1);***///目标数据表bulkCopy.DestinationTableName="Fina_ReceDetail";//每次更新五条bulkCopy.BatchSize=5;//每更新十条的时候就发出通知bulkCopy.NotifyAfter=10;//下面的函数随时都在监督着这里bulkCopy.SqlRowsCopied+=newSqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied);bulkCopy.WriteToServer(reader);}Target.Close();}sourceConnection.Close();reader.Close();}Console.WriteLine("这次数据导入共用了:"+st.ElapsedMilliseconds);st.Stop();}staticvoidbulkCopy_SqlRowsCopied(objectsender,SqlRowsCopiedEventArgse){Console.WriteLine("已经有"+e.RowsCopied+"条数据被导入");}}}
我现在有个问题,第一个表导入第二个表的时候,第二个表有一些额外的数据字段,这些数据字段的数据都是按规则生成的!!(比如:现在时间的毫秒数+ReceiveOrder+自动增长列+现在登录的人的姓名)等这样的规则,那如何在导入数据的时候自动增加呢!!
解决方案
本帖最后由 TianMaiCheng 于 2012-06-05 11:10:30 编辑
解决方案二:
自己拼装其他的字段。