DAL层方法

  /// <summary> 
        ///为执行命令做参数准备     
        ///</summary>       
        ///<param >SqlCommand object</param>   
        ///<param >SqlConnection object</param>       
        ///<param >SqlTransaction object</param>       
        ///<param >一个存储过程,或者sql语句类型</param>     
        ///<param >一个命令sql语句</param>        
        ///<param >参数集合</param>       
        public static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms)
        {
            try
            {
                //判断连接的状态。如果是关闭状态,则打开                
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                //cmd属性赋值                
                cmd.Connection = conn;
                cmd.CommandText = cmdText;                //是否需要用到事务处理      
                if (trans != null)
                    cmd.Transaction = trans;
                cmd.CommandType = cmdType;                //添加cmd需要的存储过程参数   
                if (cmdParms != null)
                {
                    foreach (SQLiteParameter parm in cmdParms)
                        cmd.Parameters.Add(parm);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

 

BLL层方法

        /// <summary>        
        /// 执行多条SQL语句,实现数据库事务。        
        /// </summary>        
        /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SQLiteParameter[])</param>        
        public static void ExecuteSqlTran(Hashtable SQLStringList, ConnectionType type)
        {
            SQLiteConnection conn = GetConnection(type);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            using (SQLiteWriteLock sqliteLock = new SQLiteWriteLock(connString))
            {
                using (SQLiteTransaction trans = conn.BeginTransaction())
                {
                    SQLiteCommand cmd = new SQLiteCommand();
                    try
                    {
                        bool hadExce = false;
                        //循环                    
                        foreach (DictionaryEntry myDE in SQLStringList)
                        {
                            string cmdText = myDE.Key.ToString();
                            SQLiteParameter[] cmdParms = (SQLiteParameter[])myDE.Value;
                            PrepareCommand(cmd, conn, trans, CommandType.Text, cmdText, cmdParms);
                            int val = cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            hadExce = true;
                        }
                        if (hadExce)
                        {
                            trans.Commit();
                        }

                    }
                    catch (System.Data.SQLite.SQLiteException e)
                    {
                        trans.Rollback();
                        conn.Close();
                        throw new Exception(e.Message);
                    }
                }
            }
        }

UI层

 Hashtable hashtb = new Hashtable();
                    for (int i = 0; i < dtOrderDetail.Rows.Count; i++)
                    {
                        string prodcode = dtOrderDetail.Rows[i]["?"].ToString();
                        string colorcode = dtOrderDetail.Rows[i]["?"].ToString();
                        string pattern = dtOrderDetail.Rows[i]["?"].ToString();
                        DateTime WNo = (DateTime)dtOrderDetail.Rows[i]["?"];
                        SQLiteParameter[] parameters = {
                            new SQLiteParameter("@?", DbType.String),
                            new SQLiteParameter("@?", DbType.String),
                            new SQLiteParameter("@?", DbType.String),
                            new SQLiteParameter("@?", DbType.DateTime)};
                        parameters[0].Value = ?;
                        parameters[1].Value = ?;
                        parameters[2].Value = ?;
                        parameters[3].Value = ?;
                        hashtb.Add("update OrderDetail set ? where ? and ? and ?and ? and ?and " + i + "=" + i, parameters);
                    }
SqliteClass.ExecuteSqlTran(hashtb, SqliteClass.ConnectionType.Biz);