日期:2014-05-17  浏览次数:20418 次

.net循环插入数据
WebReference.IHotelService myclient = new WebReference.IHotelService();
            object dateinfo = myclient.getHotelDataInfoList();


object里面获取了一个数据集。理由8000多条数据,怎样把这数据循环插入到sql数据库表?
------解决方案--------------------
datatable批量插入,但前提是datatable里面的列字段的数据类型、长度要跟待插入表的一致。
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(__connStr))
               {
                    try
                    {
                         sqlBulkCopy.DestinationTableName = DestinationTableName;
                         sqlBulkCopy.BatchSize = dt.Rows.Count;
                         __sqlConn.Open();
                         if (dt != null && dt.Rows.Count != 0)
                         {
                              sqlBulkCopy.WriteToServer(dt);
                         }
                         sqlBulkCopy.Close();
                         __sqlConn.Close();
                    }
                    catch
                    {
                         return false;
                    }
               }
               return true;