C# 執行事務

public bool ExecuteSqlTransaction(string[] strSQL)
        {
            SqlCommand sc = new SqlCommand(strSQL[0], con);
            sc.CommandType = CommandType.Text;
            try
            {
                sc.Connection.Open();
            }
            catch
            {
                //MessageBox.Show("與數據庫通訊異常!系統將非正常退出!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            using (SqlTransaction trans = con.BeginTransaction())
            {
                sc.Transaction = trans;
                try
                {
                    for (int i = 0; i < strSQL.Length; i++)
                    {
                        sc.CommandText = strSQL[i];


                        sc.ExecuteNonQuery();
                    }
                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                    return false;
                    throw;
                }
                finally
                {
                    con.Close();
                }
            }
            return true;
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章