【SQLite】 中事务使用案列

只为自己方便使用。

View Code
using (SQLiteConnection conn = new SQLiteConnection(SQLiteHelper.connectionString))
            {
                conn.Open();
                using (SQLiteTransaction trans = conn.BeginTransaction())
                {
                    SQLiteCommand cmd = new SQLiteCommand();
                    try
                    {
                        cmd.Connection = conn;
                        cmd.CommandType = CommandType.Text;
                        cmd.Transaction = trans;
                        //创建质量屋的左墙(顾客需求)
                        for (int i = 0; i < int.Parse(txtCusRequireNum.Text); i++)
                        {
                            cmd.CommandText = "INSERT INTO CusRequireInfo(HouseId,OrderId)VALUES(" + houseId + "," + i + ")";
                            cmd.ExecuteNonQuery();
                        }
                        //创建质量屋的天花板(工程措施)
                        for (int j = 0; j < int.Parse(txtCeilingNum.Text); j++)
                        {
                            cmd.CommandText = "INSERT INTO CeilingInfo(HouseId,OrderId)VALUES(" + houseId + "," + j + ")";
                            cmd.ExecuteNonQuery();
                        }
                        //对应关系矩阵
                        List<CeilingInfo> ceilingInfos = CeilingBLL.GetModelList("HouseId=" + houseId);
                        List<QFD.Model.CusRequireInfo> cusRequiredInfos = CRBLL.GetModelList("HouseId=" + houseId);
                        for (int i = 0; i < cusRequiredInfos.Count; i++)
                        {
                            for (int j = 0; j < ceilingInfos.Count; j++)//将关系矩阵的值保存到数据库中
                            {
                                cmd.CommandText = "INSERT INTO CusRequireCeilingRelation(CeilingId,CusRequireId,RelationValue)VALUES(" + ceilingInfos[j].CeilingId + "," + cusRequiredInfos[i].CusRequireId + ","+0+")";
                                cmd.ExecuteNonQuery();
                            }
                        }
                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.Rollback();
                        throw;
                    }
                }
            }

 

posted @ 2013-02-16 13:24  Ruicky  阅读(160)  评论(0编辑  收藏  举报