SqlTransaction trans = null; SqlConnection con = new Sqlconnection("數據庫鏈接語句"); try { con.Open(); trans = con.BeginTransaction(); SqlCommand com = new SqlCommand(); //一下4步是要執行SqlParameter,若是不執行能夠直接跳過使用com.CommandText="SQL語句"; com.CommandText = @"數據庫語句 條件=@變量"; SqlParameter para = new SqlParameter("變量","值"); com.Parameters.Add(para); com.CommandText=com.CommandText; com.Connection=con; com.Transaction=trans; com.ExecuteNonQuery();//執行方式本身選擇 trans.Commit();//執行提交事務 }catch { trans.Rollback();//若是前面有異常則事務回滾 } finally { con.Close(); }