日期:2014-05-18 浏览次数:21686 次
private static void Prepare(SqlConnection con, SqlCommand cmd, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) { con.ConnectionString = ConnectionString; cmd.Connection = con; cmd.CommandType = cmdType; cmd.CommandText = cmdText; if (cmdParms != null) { foreach (SqlParameter parm in cmdParms) cmd.Parameters.Add(parm); } con.Open(); } public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) { using (SqlConnection con = new SqlConnection()) { int iResult = -1; SqlCommand cmd = new SqlCommand(); Prepare(con, cmd, cmdType, cmdText, cmdParms); SqlTransaction tran = con.BeginTransaction(); try { iResult = cmd.ExecuteNonQuery(); tran.Commit(); } catch { tran.Rollback(); throw; } return iResult; }//end using }