【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()));
                }
            }
原文链接:https://www.f2er.com/sqlite/199047.html

猜你在找的Sqlite相关文章