Example #1
2
        public override void WriteToServer(string destinationTableName, DataTable table,
            int batchSize = 0, params BulkCopyColumnMapping[] columnMappings)
        {
            OracleBulkCopy bulkCopy = null;
            try
            {
                if (DbTransaction != null)
                {
                    bulkCopy = new OracleBulkCopy((OracleConnection)DbTransaction.Connection, OracleBulkCopyOptions.Default);
                }
                else
                {
                    bulkCopy = new OracleBulkCopy(_connectionString);
                }

                bulkCopy.DestinationTableName = destinationTableName;
                if (batchSize != 0)
                {
                    bulkCopy.BatchSize = batchSize;
                }
                if (BulkCopyTimeout != (int)CommandTimeoutValue.None)
                {
                    bulkCopy.BulkCopyTimeout = BulkCopyTimeout;
                }
                if (columnMappings != null)
                {
                    foreach (var item in columnMappings)
                    {
                        var mapping = new OracleBulkCopyColumnMapping();

                        if (item.DestinationColumn != null)
                            mapping.DestinationColumn = item.DestinationColumn;
                        if (item.DestinationOrdinal != -1)
                            mapping.DestinationOrdinal = item.DestinationOrdinal;
                        if (item.SourceColumn != null)
                            mapping.SourceColumn = item.SourceColumn;
                        if (item.SourceOrdinal != -1)
                            mapping.SourceOrdinal = item.SourceOrdinal;

                        bulkCopy.ColumnMappings.Add(mapping);
                    }
                }
                bulkCopy.WriteToServer(table);
            }
            finally
            {
                if (bulkCopy != null) { bulkCopy.Close(); }
            }
        }
		/// <summary>
		/// Bulk copies a set of objects to the server.
		/// </summary>
		/// <param name="connection">The connection to use.</param>
		/// <param name="tableName">The name of the table.</param>
		/// <param name="reader">The reader to read objects from.</param>
		/// <param name="configure">A callback method to configure the bulk copy object.</param>
		/// <param name="options">Options for initializing the bulk copy object.</param>
		/// <param name="transaction">An optional transaction to participate in.</param>
		public override void BulkCopy(IDbConnection connection, string tableName, IDataReader reader, Action<InsightBulkCopy> configure, InsightBulkCopyOptions options, IDbTransaction transaction)
		{
			if (reader == null) throw new ArgumentNullException("reader");
			if (transaction != null)
				throw new ArgumentException("OracleProvider does not support external transactions for bulk copy", "transaction");

			OracleBulkCopyOptions oracleOptions = OracleBulkCopyOptions.Default;
			if (options.HasFlag(InsightBulkCopyOptions.UseInternalTransaction))
				oracleOptions |= OracleBulkCopyOptions.UseInternalTransaction;

			using (var bulk = new OracleBulkCopy((OracleConnection)connection, oracleOptions))
			using (var insightBulk = new OracleInsightBulkCopy(bulk))
			{
				bulk.DestinationTableName = tableName;

				// map the columns by name, in case we skipped a readonly column
				foreach (DataRow row in reader.GetSchemaTable().Rows)
					bulk.ColumnMappings.Add((string)row["ColumnName"], (string)row["ColumnName"]);

				if (configure != null)
					configure(insightBulk);
				bulk.WriteToServer(reader);
			}
		}
Example #3
1
 /// <summary>
 ///大批量数据插入
 /// </summary>
 /// <param name="table">数据表</param>
 /// <param name="connectionString">数据库连接字符串</param>
 /// <returns></returns>
 public bool OracleBulkInsert(DataTable table, string connectionString)
 {
     try
     {
         using (OracleConnection connection = new OracleConnection(connectionString))
         {
             connection.Open();
             OracleTransaction trans = connection.BeginTransaction();
             using (OracleBulkCopy bulkCopy = new OracleBulkCopy(connection))
             {
                 //设置源表名称
                 bulkCopy.DestinationTableName = table.TableName;
                 //设置超时限制
                 bulkCopy.BulkCopyTimeout = CommandTimeOut;
                 //要写入列
                 foreach (DataColumn dtColumn in table.Columns)
                 {
                     bulkCopy.ColumnMappings.Add(dtColumn.ColumnName.ToUpper(), dtColumn.ColumnName.ToUpper());
                 }
                 try
                 {
                     // 写入
                     bulkCopy.WriteToServer(table);
                     // 提交事务
                     trans.Commit();
                     return true;
                 }
                 catch
                 {
                     trans.Rollback();
                     bulkCopy.Close();
                     return false;
                 }
                 finally
                 {
                     connection.Close();
                     connection.Dispose();
                     bulkCopy.Close();
                     bulkCopy.Dispose();
                 }
             }
         }
     }
     catch (Exception e)
     {
         DbLog.WriteException(e);
         return false;
     }
 }
Example #4
0
        void CopyTo(DbConnection con)
        {
            using (OracleBulkCopy obc = new OracleBulkCopy(con as OracleConnection))
            {

            }
        }
			public OracleInsightBulkCopy(OracleBulkCopy bulkCopy)
			{
				if (bulkCopy == null) throw new ArgumentNullException("bulkCopy");

				_bulkCopy = bulkCopy;
				_bulkCopy.OracleRowsCopied += OnRowsCopied;
			}
		/// <summary>
		/// Bulk copies a set of objects to the server.
		/// </summary>
		/// <param name="connection">The connection to use.</param>
		/// <param name="tableName">The name of the table.</param>
		/// <param name="reader">The reader to read objects from.</param>
		/// <param name="configure">A callback method to configure the bulk copy object.</param>
		/// <param name="options">Options for initializing the bulk copy object.</param>
		/// <param name="transaction">An optional transaction to participate in.</param>
		public override void BulkCopy(IDbConnection connection, string tableName, IDataReader reader, Action<InsightBulkCopy> configure, InsightBulkCopyOptions options, IDbTransaction transaction)
		{
			if (transaction != null)
				throw new ArgumentException("OracleProvider does not support external transactions for bulk copy", "transaction");

			OracleBulkCopyOptions oracleOptions = OracleBulkCopyOptions.Default;
			if (options.HasFlag(InsightBulkCopyOptions.UseInternalTransaction))
				oracleOptions |= OracleBulkCopyOptions.UseInternalTransaction;

			using (var bulk = new OracleBulkCopy((OracleConnection)connection, oracleOptions))
			using (var oracleBulk = new OracleInsightBulkCopy(bulk))
			{
				bulk.DestinationTableName = tableName;
				if (configure != null)
					configure(oracleBulk);
				bulk.WriteToServer(reader);
			}
		}
Example #7
0
        public bool SqlBulkCopyImport(System.Collections.Generic.IList<string> maplist, string TableName, DataTable dt)
        {
            using (OracleConnection connection = new OracleConnection(connectionString))
            {

                connection.Open();

                using (OracleBulkCopy bulkCopy = new OracleBulkCopy(connection))
                {

                    bulkCopy.DestinationTableName = TableName;

                    foreach (string a in maplist)
                    {

                        bulkCopy.ColumnMappings.Add(a, a);

                    }

                    try
                    {

                        bulkCopy.WriteToServer(dt);

                        return true;

                    }

                    catch (Exception e)
                    {

                        throw e;

                    }
                }
            }
        }
Example #8
0
 /// <summary>
 /// 快速导入dataTable映射的列至数据库表(目标列名区分大小写)
 /// </summary>
 /// <param name="dataTable">要导入的dataTable</param>
 /// <param name="tableName">目标表名</param>
 /// <param name="columnMapping">列映射关系</param>
 /// <returns>成功返回success,否则为返回错误消息</returns>
 public string FastImportDatatableToDB(DataTable dataTable, string tableName, DataRow[] columnMapping)
 {
     OracleBulkCopy bulkCopy = new OracleBulkCopy(this._strConnString, OracleBulkCopyOptions.UseInternalTransaction);
     bulkCopy.DestinationTableName = tableName;
     foreach (DataRow row in columnMapping)
     {
         bulkCopy.ColumnMappings.Add(row[0].ToString(), row[1].ToString());
     }
     OpenConn();
     bulkCopy.WriteToServer(dataTable);
     return "success";
 }
Example #9
0
 public OracleBulkCopyWrapper(DbConnection conn)
 {
     OracleConnection sqlConn = conn as OracleConnection;
     bulkCopy = new OracleBulkCopy(sqlConn);
     this.ColumnMappings = new Dictionary<string, string>();
 }
 //Interface para o w(DataTable) do ODP.NET
 //http://docs.oracle.com/html/E10927_01/OracleBulkCopyClass.htm#BIGCDJDD
 public bool WriteToServer(DataTable dados, string tabela)
 {
     ClearMessages();
     OracleBulkCopy bulkCopy = new OracleBulkCopy(myConnection);
     bulkCopy.DestinationTableName = tabela;
     bulkCopy.BulkCopyTimeout = 3600;
     try
     {
         bulkCopy.WriteToServer(dados);
     }
     catch (Exception e)
     {
         ERROR = e.Message;
         if (e.InnerException != null)
         {
             ERROR += e.InnerException.Message;
         }
         return false;
     }
     bulkCopy.Dispose();
     return true;
 }
 /// <summary>
 /// Does the actual bulk inserts.
 /// </summary>
 /// <param name="table"></param>
 /// <param name="table_name"></param>
 /// <param name="batch_size"></param>
 private void BulkCopy(DataTable table, string table_name, int batch_size)
 {
     if (table != null && table.Rows.Count > 0)
     {
         OracleBulkCopy bulk = new OracleBulkCopy(_connection, OracleBulkCopyOptions.Default);
         bulk.BatchSize = batch_size;
         bulk.DestinationTableName = table_name;
         bulk.WriteToServer(table);
         bulk.Dispose();
         bulk = null;
         Console.WriteLine("Inserted {0} records into {1}!", table.Rows.Count, table_name);
     }
 }
Example #12
0
        public override void Load(string table, DataTable records, int bachSize, int timeout)
        {
            using (var oraBulkCopy = new OracleBulkCopy(ConnectionString))
            {
                // set bulk copy options
                oraBulkCopy.BulkCopyOptions = OracleBulkCopyOptions.Default;

                // mapping between data source and target database table
                oraBulkCopy.DestinationTableName = table;

                // batch and time options
                oraBulkCopy.BatchSize = bachSize;
                oraBulkCopy.BulkCopyTimeout = timeout;  // in seconds; i've set my time out to 10 minutes

                // do the actual copy
                oraBulkCopy.WriteToServer(records);
            }
        }
Example #13
-1
        public void InsertBulkEmail(List<Email> emailList)
        {
            string connectionString = OracleConnectionString;
            try
            {
                DataTable smsTable = GetEmailTable(emailList);
                OracleBulkCopy bulkCopy = new OracleBulkCopy(connectionString);
                bulkCopy.ColumnMappings.Add("Id", "Id");
                bulkCopy.ColumnMappings.Add("ToAddress", "ToAddress");
                bulkCopy.ColumnMappings.Add("Subject", "Subject");
                bulkCopy.ColumnMappings.Add("StatusId", "StatusId");
                bulkCopy.ColumnMappings.Add("StatusDate", "StatusDate");
                bulkCopy.ColumnMappings.Add("CampaignId", "CampaignId");
                bulkCopy.ColumnMappings.Add("Body", "Body");
                bulkCopy.DestinationTableName = "EMAIL";
                bulkCopy.WriteToServer(smsTable);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #14
-1
 protected void InsertDataToDbBulkMethod(DataTable table)
 {
     OracleBulkCopy bcopy = new OracleBulkCopy(_conn as OracleConnection);
     bcopy.DestinationTableName = table.TableName;
     bcopy.WriteToServer(table);
     bcopy.Close();
 }
Example #15
-1
        public void InsertBulkSMS(List<Sms> smsList)
        {
            string connectionString = OracleConnectionString;
            try
            {
                DataTable smsTable = GetSmsTable(smsList);
                OracleBulkCopy bulkCopy = new OracleBulkCopy(connectionString);
                bulkCopy.ColumnMappings.Add("Id", "Id");
                bulkCopy.ColumnMappings.Add("ToNumber", "ToNumber");
                bulkCopy.ColumnMappings.Add("Content", "Content");
                bulkCopy.ColumnMappings.Add("StatusId", "StatusId");
                bulkCopy.ColumnMappings.Add("StatusDate", "StatusDate");
                bulkCopy.ColumnMappings.Add("CampaignId", "CampaignId");
                bulkCopy.DestinationTableName = "SMS";
                bulkCopy.WriteToServer(smsTable);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }