標籤:try 查詢 new ring bsp value 直接 except 串連
注意
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
直接將DataTable存入oracle資料庫中(轉)