码迷,mamicode.com
首页 > 其他好文 > 详细

批量插入与更新随记

时间:2017-05-04 18:39:36      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:批量插入   isp   upd   int   ted   rom   cat   turn   row   

一、批量插入数据:

     int times = 0;

     for (int count = 0; count < dt.Rows.Count; times++)

    {

       for (int i = 0; i < 400 && 400 * times + i < dt.Rows.Count; i++, count++)

         {

             dtTemp.Rows.Add(dt.Rows[count].ItemArray);

         } myDataAdapter.Update(dtTemp);

       dtTemp.Rows.Clear();

     }

 

二、批量更新数据:

    public static bool MultiUpdateData(DataTable data, string Columns, string tableName)

{

      using (OracleConnection connection = new OracleConnection(connectionString))

    {

          string SQLString = string.Format("select {0} from {1} where rownum=0", Columns, tableName);

          using (OracleCommand cmd = new OracleCommand(SQLString, connection))

          {

               try {

                        connection.Open();

                        OracleDataAdapter myDataAdapter = new OracleDataAdapter();

                        myDataAdapter.SelectCommand = new OracleCommand(SQLString, connection);

                       OracleCommandBuilder custCB = new OracleCommandBuilder(myDataAdapter);

                       custCB.ConflictOption = ConflictOption.OverwriteChanges;

                       custCB.SetAllValues = true;

                    foreach (DataRow dr in data.Rows)

                    {

                          if (dr.RowState == DataRowState.Unchanged)

                              dr.SetModified();

                    }

                          myDataAdapter.Update(data);

                         data.AcceptChanges();

                          myDataAdapter.Dispose();

                        return true;

                 } catch (System.Data.OracleClient.OracleException E)

                  {

                       connection.Close();

                   return false;

                   }

               }

         }

      }

批量插入与更新随记

标签:批量插入   isp   upd   int   ted   rom   cat   turn   row   

原文地址:http://www.cnblogs.com/Vincent-pei/p/6808413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!