Oracle BulkCopy 实现datatable整体写入表,DataTable与目标表的字段不完全对应也没关系,只要datatable中所有字段在目标表中存在就行

BulkCopy需要引用1个dll,debug目录下复制8个dll

需添加引用的1个dll :Oracle.DataAccess.dll

不需引用,需要拷贝到debug下的8个dll: oci.dll、ocijdbc11.dll、ociw32.dll、orannzsbb11.dll、oraocci11.dll、oraociei11.dll、OraOps11w.dll、orasql11.dll

 

public static bool BulkCopyData(DataTable dt, string tableName)

        {

            string OracleConnectionString = DBFactorySingleton.GetInstance().Factory.GetConnection().ConnectionString;

             string errColString = "";

            using (OracleConnection oracleconn = new OracleConnection(OracleConnectionString))

            {

                using (OracleBulkCopy bulkCopy = new OracleBulkCopy(OracleConnectionString))

                {

                    try

                    {

                        if (dt != null && dt.Rows.Count > 0)

                        {

                            bulkCopy.BatchSize = 50000;

                            bulkCopy.BulkCopyTimeout = 60;

                            bulkCopy.DestinationTableName = "AISN1." + tableName;

                            for (int i = 0; i < dt.Columns.Count; i++)

                            {

                                string col = dt.Columns[i].ColumnName;

                                errColString += col + ",";

                                bulkCopy.ColumnMappings.Add(col, col);

                            }

 

                            //提交到服务器

                            oracleconn.Open();

                            bulkCopy.WriteToServer(dt);

                        }

                    }

                    catch (Exception ex)

                    {

                        Common_FormAndControl.Message(tableName + "表数据快速导入功能出错~" + errColString + ";" + ex.ToString());

                        return false;

                    }

                    finally

                    {

                        GC.Collect();

                    }

                }

            }

posted on 2016-11-08 11:57  mol1995  阅读(1227)  评论(0编辑  收藏  举报

导航