【SQLite】使用事务处理带参数的插入
时间:2021-07-01 10:21:17
帮助过:3人阅读
(SQLiteConnection conn =
new SQLiteConnection(String.Format(
"Data Source={0};Pooling=true",fileName.ToString())))
{
using (SQLiteCommand cmd =
new SQLiteCommand())
{
conn.Open();
cmd.Connection =
conn;
Stopwatch warch =
new Stopwatch();
warch.Start();
SQLiteTransaction trans =
conn.BeginTransaction();
cmd.Transaction =
trans;
try
{
for (
int i =
0; i < _dataTable.Rows.Count; i++
)
{
cmd.CommandText =
"insert into HistoryData (PointId,PointType,TempValue,HumiValue,TTime) values (@PointId,@PointType,@TempValue,@HumiValue,@TTime)";
cmd.Parameters.Add(new SQLiteParameter(
"@PointId", DbType.Int32));
cmd.Parameters.Add(new SQLiteParameter(
"@PointType", DbType.String,
20));
cmd.Parameters.Add(new SQLiteParameter(
"@TempValue", DbType.Double));
cmd.Parameters.Add(new SQLiteParameter(
"@HumiValue", DbType.Double));
cmd.Parameters.Add(new SQLiteParameter(
"@TTime", DbType.DateTime));
cmd.Parameters[0].Value = _dataTable.Rows[i][
"PointId"].ToString();
cmd.Parameters[1].Value = _dataTable.Rows[i][
"PointType"].ToString();
cmd.Parameters[2].Value = _dataTable.Rows[i][
"TempValue"].ToString();
cmd.Parameters[3].Value = _dataTable.Rows[i][
"HumiValue"].ToString();
cmd.Parameters[4].Value = _dataTable.Rows[i][
"TTime"];
cmd.ExecuteNonQuery();
}
trans.Commit();
}
catch (Exception)
{
trans.Rollback();
throw;
}
warch.Stop();
MessageBox.Show(string.Format(
"{0},{1}",_dataTable.Rows.Count,
warch.Elapsed.ToString()));
}
}
使用事务处理插入sqlite数据库中的语句
【SQLite】使用事务处理带参数的插入
标签: