1. 程式人生 > >【SQLite】使用事務處理帶引數的插入

【SQLite】使用事務處理帶引數的插入

using (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資料庫中的語句