日期:2014-05-17 浏览次数:20401 次
public class SqlHelper { private static SqlConnection cn = null; private static SqlCommand cmd = null; public SqlHelper() { } /// <summary> /// 判断连接状态 /// </summary> /// <returns>返回连接状态</returns> private static SqlConnection GetConn() { string ConnStr = M_Common.Helper.ConfigHelper.GetconnMsSql.ToString(); cn = new SqlConnection(ConnStr); if (cn.State != ConnectionState.Open) { cn.Open(); } return cn; } /// <summary> /// 获取某表的某个字段的最大值 /// </summary> /// <param name="FieldName">字段名</param> /// <param name="TableName">表明</param> /// <returns>返回最大值</returns> public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = SqlHelper.GetSingle(strsql); if (obj == null) { return 1; } else { return int.Parse(obj.ToString()); } } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public static object GetSingle(string SQLString) { using (SqlCommand cmd = new SqlCommand(SQLString, GetConn())) { try { object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { throw e; } } } /// <summary> /// 带参数返回一行一列ExecuteScalar /// </summary> /// <param name="cmdtext">存储过程或者SQL语句</param> /// <param name="para">参数数组</param> /// <param name="ct">命令类型</param> /// <returns>返回一行一列value</returns> public static int ExecuteScalar(string cmdtext, SqlParameter[] para, CommandType ct) { int value; t