- public static object ExecuteScalar(string sql, params SqlParameter[] cmdParams)//查询返回第一行第一列 object
- {
- SqlCommand cmd = new SqlCommand(sql, conn);
- foreach (SqlParameter parm in cmdParams)
- {
- cmd.Parameters.Add(parm); //这里添加参数
- }
- try
- {
- conn.Open();
- object o = cmd.ExecuteScalar();
- return o;
- }
- catch
- {
- return null;
- }
- finally
- {
- cmd.Dispose();
- conn.Close();
- conn.Dispose();
- }
- }
复制代码 |