[C#] 纯文本查看 复制代码 public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
using (var conn = new SqlConnection(connString))
{
var cmd = new SqlCommand();
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParams);
var count = cmd.ExecuteNonQuery();
//cmd.Parameters.Clear();
return count;
}
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParams)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
if (trans != null)
cmd.Transaction = trans;
if (cmdParams != null)
{
foreach (var parm in cmdParams)
{
cmd.Parameters.Add(parm);
}
}
}
参考一下
|