1、建立List用于存放多条语句
////// 保存表单 /// /// protected void save() { Listlist = new List (); list.Add(string.Format("insert into picsone(model,idser,idflg,lmuser,lmdate,lmtime) values('{0}','{1}','{2}','{3}',{4},{5})", "T1002", "Y", "N", "U001", 20161103, 140025)); list.Add(string.Format("insert into picstwo(model,idser,idflg,lmuser,lmdate,lmtime) values('{0}','{1}','{2}','{3}',{4},{5})", "T1002", "Y", "N", "U001", 20161103, 140025)); bool bol = ExecuteTransaction(list); if (bol) { MessageBox.Show("保存成功!"); } else { MessageBox.Show("保存失败!"); } }
2、调用ExecuteTransaction方法,并返回返回值true为成功,false为失败,语句并回滚
////// 执行语句 /// /// ///private bool ExecuteTransaction(List list) { using (SqlConnection cOnnection= new SqlConnection(ConfigurationManager.AppSettings["LocalConnectionString"].ToString())) { SqlCommand command = new SqlCommand(); SqlTransaction transaction = null; try { connection.Open(); transaction = connection.BeginTransaction(); command.COnnection= connection; command.Transaction = transaction; for (int i = 0; i