c# oracle 批量更新
时间: 2023-07-04 20:16:59 浏览: 175
C# 批量插入和更新数据
4星 · 用户满意度95%
你可以使用以下代码来实现 C# 中的 Oracle 批量更新:
```csharp
using Oracle.ManagedDataAccess.Client;
using System.Data;
public void BatchUpdate()
{
string connectionString = "Data Source=<your data source>;User ID=<your user id>;Password=<your password>";
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
using (OracleCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = "UPDATE table_name SET column1 = :column1 WHERE id = :id";
OracleParameter column1Param = new OracleParameter(":column1", OracleDbType.Varchar2);
OracleParameter idParam = new OracleParameter(":id", OracleDbType.Int32);
command.Parameters.Add(column1Param);
command.Parameters.Add(idParam);
// Create a DataTable with the data to be updated
DataTable dataToUpdate = new DataTable();
dataToUpdate.Columns.Add("Column1", typeof(string));
dataToUpdate.Columns.Add("Id", typeof(int));
// Populate the DataTable with data
dataToUpdate.Rows.Add("new value 1", 1);
dataToUpdate.Rows.Add("new value 2", 2);
dataToUpdate.Rows.Add("new value 3", 3);
// Use OracleDataAdapter to update the data in batches
using (OracleDataAdapter adapter = new OracleDataAdapter())
{
adapter.UpdateBatchSize = 100; // Update 100 rows at a time
adapter.UpdateCommand = command;
adapter.Update(dataToUpdate);
}
}
}
}
```
这个示例代码演示了如何使用 OracleDataAdapter 来批量更新数据,你只需要将示例中的数据替换为你自己的数据源和表格名称即可。
阅读全文