直接将DataTable存入oracle数据库中(转)
注意
1:传入的DataTable的列必须和数据库中表列必须一致,否则数据会默认往前几列存
2:sql语句只要是对要插入的表的一个查询,目的是为了确定表名
3:取得连接字符串的方法为GetOracleConnection(),需要自己添加
public static bool insertValueWithDt(DataTable dataTable, string sql)
{
String ConnStr = GetOracleConnection();
using (OracleConnection conn = new OracleConnection(ConnStr))
{
try
{
OracleCommand cmd = new OracleCommand(sql,conn);
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
OracleCommandBuilder cb = new OracleCommandBuilder(adapter);
DataTable dsNew = new DataTable();
int count = adapter.Fill(dsNew);
for (int i = 0; i < dataTable.Rows.Count; i++)
{
DataRow dr = dsNew.NewRow();
for (int j = 0; j < dataTable.Columns.Count; j++)
{
dr[dsNew.Columns[j].ColumnName] = dataTable.Rows[i][j];
}
dsNew.Rows.Add(dr);
}
count = adapter.Update(dsNew);
adapter.UpdateBatchSize = 200;
//adapter.Update(dataTable);
return true;
}
catch (Exception e)
{
WriteLog writeLog = new WriteLog();
writeLog.WriteLogs(e.ToString());
return false;
}
}
}
来自:http://blog.sina.com.cn/s/blog_877c72ac010126h1.html