发布网友 发布时间:2023-11-12 21:01
共1个回答
热心网友 时间:2024-12-05 01:58
string selectCommand =/// <summary>
/// 保存DataTable
/// </summary>
/// <param name="dataTable">DataTable</param>
/// <param name="tableName">表名</param>
/// <param name="commandText">sql文本命名</param>
/// <param name="continueUpdateOnError">行更新过程中遇到错误是否成异常</param>
/// <returns>错误行集合</returns>
public static DataRow[] SaveDataTable(DataTable dataTable, string tableName, string commandText,
bool continueUpdateOnError)
{
var helper = SQLiteDBHelperService.Instanse();
var trans = helper.BeginTrans();
var conn = trans.Connection;
if (conn.State == ConnectionState.Closed)
conn.Open();
try
{
var cmd = conn.CreateCommand();
cmd.CommandText = commandText;
cmd.Connection = conn;
cmd.Transaction = trans;
var adp = helper.DBComm.CreateDataAdapter(cmd);
adp.ContinueUpdateOnError = continueUpdateOnError;
DbCommandBuilder cmdBuilder = helper.DBComm.CreateCommandBuilder(adp);
cmdBuilder.DataAdapter = adp;
cmdBuilder.QuotePrefix = "[";
cmdBuilder.QuoteSuffix = "]";
adp.InsertCommand = cmdBuilder.GetInsertCommand();
adp.UpdateCommand = cmdBuilder.GetUpdateCommand();
adp.DeleteCommand = cmdBuilder.GetDeleteCommand();
adp.Update(dataTable);
dataTable.AcceptChanges();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
MessageBox.Show(ex.ToString());
}
conn.Close();
return dataTable.GetErrors();
}