直接將DataTable存入oracle資料庫中

來源:互聯網
上載者:User

標籤:try   查詢   new   ring   bsp   value   直接   except   串連   

注意

    1:傳入的DataTable的列必須和資料庫中表列必須一致,否則資料會預設往前幾列存

    2:sql語句只要是對要插入的表的一個查詢,目的是為了確定表名

   3:取得連接字串的方法為GetOracleConnection(),需要自己添加

public static bool insertValueWithDt(DataTable dataTable, string sql)

        {

            String ConnStr = GetOracleConnection();

            using (OracleConnection conn = new OracleConnection(ConnStr))

            {

                try

                {

                    OracleCommand cmd = new OracleCommand(sql,conn);

                    OracleDataAdapter adapter = new OracleDataAdapter(cmd);

                    OracleCommandBuilder cb = new OracleCommandBuilder(adapter);

                    DataTable dsNew = new DataTable();

                    int count = adapter.Fill(dsNew);

                    for (int i = 0; i < dataTable.Rows.Count; i++)

                    {

                        DataRow dr = dsNew.NewRow();

                        for (int j = 0; j < dataTable.Columns.Count; j++)

                        {

                            dr[dsNew.Columns[j].ColumnName] = dataTable.Rows[i][j];

                        }

                        dsNew.Rows.Add(dr);

                    }

                    count = adapter.Update(dsNew);

                    adapter.UpdateBatchSize = 200;

                    //adapter.Update(dataTable);

                    return true;

                }

                catch (Exception e)

                {

                    WriteLog writeLog = new WriteLog();

                    writeLog.WriteLogs(e.ToString());

                    return false;

                }

            }

        }

來自:http://blog.sina.com.cn/s/blog_877c72ac010126h1.html

直接將DataTable存入oracle資料庫中(轉)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.