我已经阅读了很多与此有关的问题,但找不到能批量插入的问题。
我使用以下代码从Oracle数据库插入到MySQL数据库中:
选择数据:
public DataTable GetDataTableLayout(string tableName)
{
DataTable table = new DataTable();
var connectionString = @"";
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
string query = $"select ID, EVENTO, DT_EVENTO, cd_categoria_abrasce, nm_categoria_abrasce from BI_STG.STG_CRM_CATEGORIA_ABRASCE";
using (OracleDataAdapter adapter = new OracleDataAdapter(query, connection))
{
adapter.Fill(table);
};
}
return table;
}
插入数据:
public void BulkInsertMySQL(DataTable table, string tableName)
{
var connectionString = @"";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
using (MySqlTransaction tran = connection.BeginTransaction(IsolationLevel.Serializable))
{
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.Connection = connection;
cmd.Transaction = tran;
cmd.CommandText = $"INSERT INTO clog_crm_categoria_abrasce" +
"(ID, EVENTO, DT_EVENTO, cd_categoria_abrasce, nm_categoria_abrasce)" +
"VALUES (ID, EVENTO, DT_EVENTO, cd_categoria_abrasce, nm_categoria_abrasce) from BI_STG.STG_CRM_CATEGORIA_ABRASCE";
using (MySqlDataAdapter adapter = new MySqlDataAdapter(cmd))
{
adapter.UpdateBatchSize = 1000;
using (MySqlCommandBuilder cb = new MySqlCommandBuilder(adapter))
{
cb.SetAllValues = true;
adapter.Update(table);
tran.Commit();
}
};
}
}
}