.NET大批量插入數據到Oracle

跟一個第三方系統作接口,須要插入幾百萬條數據到Oracle數據庫。html

下載Oracle的Managed版本的ODP.NET組件,只須要一個Oracle.ManagedDataAccess.dll這個DLL就能夠搞定了,使用數組參數的方式,能夠毫秒級插入幾百萬條數據。
下載地址
http://www.oracle.com/technetwork/database/windows/downloads/index-090165.html
示例代碼以下:sql

string[] codeValue = new string[shopList.Count];
                            string[] nameValue = new string[shopList.Count];
                           
                            int j = 0;
                            foreach (Shop shop in shopList)
                            {
                                codeValue[j] = shop.Code;
                                nameValue[j] = shop.CName;
                    
                               j++;
                            }
                            command = new OracleCommand(sql, connection);

                            sql = string.Format("INSERT INTO SHOP_ALL VALUES(:Code,:CName)");
                            command.ArrayBindCount = shopList.Count;            //批量插入記錄的條數,必定要賦值
                            command.CommandText = sql;

                            OracleParameter codePara = new OracleParameter("Code", OracleDbType.NVarchar2);
                            codePara.Direction = ParameterDirection.Input;
                            codePara.Value = codeValue;
                            command.Parameters.Add(codePara);

                            OracleParameter nameParameter = new OracleParameter("CName", OracleDbType.NVarchar2);
                            nameParameter.Direction = ParameterDirection.Input;
                            nameParameter.Value = nameValue;
                            command.Parameters.Add(nameParameter);

                            command.ExecuteNonQuery();
相關文章
相關標籤/搜索