- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- /// <summary>
- /// DBmanager的摘要说明
- /// </summary>
- public class DBmanager
- {
- private static readonly string sqlCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- protected static SqlConnection GetSqlconnection()//返回SqlConnection对象;
- {
- SqlConnection sqlcon = new SqlConnection(sqlCon);
- return sqlcon;
- }
- //
- // TODO:
- /// <summary>
- /// 检测记录是否存在
- /// </summary>
- /// <param name="CheckedField">被检测的字段</param>
- /// <param name="CheckedContent">字段的内容</param>
- /// <param name="CheckedTable">被检测的字段所在的表</param>
- /// <returns>True:存在,False:不存在</returns>
- public static bool IsExistRecord(string CheckedField, string CheckedContent, string CheckedTable)
- {
- string IsExistConditon = "where " + CheckedField + "=" + CheckedContent + "";
- string IsExistSQL = "select " + CheckedField + " from " + CheckedTable + " " + IsExistConditon + "";
- SqlDataReader myReader = Static_getDataReadBySql(IsExistSQL);
- if (myReader.Read())
- {
- if (myReader != null) { myReader.Close(); }
- return true;//存在
- }
- else
- {
- if (myReader != null) { myReader.Close(); }
- return false;//不存在
- }
- }
- /// <summary>
- /// 根据SQL语句返回一个数据集对象
- /// </summary>
- /// <param name="Sqlcmd">要生成数据集对象的SqlCommand</param>
- /// <returns>返回值:DataSet</returns>
- public static DataSet Static_getDataSet(SqlCommand Sqlcmd)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- //sql命令
- Sqlcmd.Connection = sqlCon;
- //适配器
- SqlDataAdapter sqlData = new SqlDataAdapter();
- sqlData.SelectCommand = Sqlcmd;
- DataSet ds = new DataSet();
- sqlData.Fill(ds);
- return ds;
- }
- /// <summary>
- /// 根据SQL语句返回一个数据集对象
- /// </summary>
- /// <param name="sqlstr">要生成数据集对象的string</param>
- /// <returns>返回值:DataSet</returns>
- public static DataSet Static_getDataSet(string sqlstr)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = sqlstr;
- //sql命令
- cmd.Connection = sqlCon;
- //适配器
- SqlDataAdapter sqlData = new SqlDataAdapter();
- sqlData.SelectCommand = cmd;
- DataSet ds = new DataSet();
- sqlData.Fill(ds);
- return ds;
- }
- /// <summary>
- /// 根据SQL语句返回一个数据集对象
- /// </summary>
- /// <param name="sqlstr">要生成数据集对象的string</param>
- /// <param name="tbname">指定返回数据集的表名称</param>
- /// <returns>返回值:DataSet</returns>
- public static DataSet Static_getDataSet(string sqlstr, string tbname)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = sqlstr;
- //sql命令
- cmd.Connection = sqlCon;
- //适配器
- SqlDataAdapter sqlData = new SqlDataAdapter();
- sqlData.SelectCommand = cmd;
- DataSet ds = new DataSet();
- sqlData.Fill(ds, tbname);
- return ds;
- }
- /// <summary>
- /// 根据SQL语句返回一个数据视图对象
- /// </summary>
- /// <param name="Sqlcmd">要生成数据集对象的SqlCommand</param>
- /// <returns>返回值:DataView</returns>
- public static DataView Static_getDataView(SqlCommand Sqlcmd)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- //sql命令
- Sqlcmd.Connection = sqlCon;
- //适配器
- SqlDataAdapter sqlData = new SqlDataAdapter();
- sqlData.SelectCommand = Sqlcmd;
- DataSet ds = new DataSet();
- sqlData.Fill(ds);
- return ds.Tables[0].DefaultView;
- }
- /// <summary>
- /// 根据SqlCommand对象的实例返回SqlDataAdapter对象
- /// </summary>
- /// <param name="Sqlcmd">SqlCommand对象</param>
- /// <returns>SqlDataAdapter SqlDataAdapter对象的实例</returns>
- public static SqlDataAdapter Static_getDataAdapter(SqlCommand Sqlcmd)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- //sql命令
- Sqlcmd.Connection = sqlCon;
- //适配器
- SqlDataAdapter sqlData = new SqlDataAdapter();
- sqlData.SelectCommand = Sqlcmd;
- return sqlData;
- }
- /// <summary>
- /// 根据传递过来的参数返回一个数据读对象
- /// </summary>
- /// <param name="strSql">String SQL语句</param>
- /// <returns>SqlDataReader SqlDataReader对象的实例</returns>
- public static SqlDataReader Static_getDataReadBySql(string strSql)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- //sql命令
- SqlCommand sqlCmd = new SqlCommand();
- sqlCmd.CommandType = CommandType.Text;
- sqlCmd.CommandText = strSql;
- sqlCmd.Connection = sqlCon;
- //
- sqlCon.Open();
- SqlDataReader sqlRead = sqlCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
- //sqlCon.Close();
- return sqlRead;
- }
- /// <summary>
- /// 根据传递过来的参数返回一个数据读对象
- /// </summary>
- /// <param name="Conn">SqlConnection 数据库连接对象</param>
- /// <param name="SQL">String SQL语句</param>
- /// <returns>SqlDataReader SqlDataReader对象的实例</returns>
- public static SqlDataReader Static_GetSqlDataReader(SqlConnection Conn, String Str_SQL)
- {
- //连接对象
- SqlConnection sqlCon = Conn;
- //sql命令
- SqlCommand sqlCmd = new SqlCommand();
- sqlCmd.CommandType = CommandType.Text;
- sqlCmd.CommandText = Str_SQL;
- sqlCmd.Connection = sqlCon;
- //
- sqlCon.Open();
- SqlDataReader sqlRead = sqlCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
- //sqlCon.Close();
- return sqlRead;
- }
- /// <summary>
- /// 执行传递过来的SQL语句
- /// </summary>
- /// <param name="strSql">string 要执行的SQL语句</param>
- public static void Static_excuteSql(string strSql)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- //sql命令
- SqlCommand sqlCmd = new SqlCommand();
- sqlCmd.CommandType = CommandType.Text;
- sqlCmd.CommandText = strSql;
- sqlCmd.Connection = sqlCon;
- sqlCon.Open();
- sqlCmd.ExecuteNonQuery();
- sqlCon.Close();
- }
- /// <summary>
- /// 执行传递过来的SQL语句返回受影响的行数
- /// </summary>
- /// <param name="strSql">string 要执行的SQL语句</param>
- /// <returns>int 受影响的行数</returns>
- public static int Static_excuteNonQuerySqlCount(string strSql)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- //sql命令
- SqlCommand sqlCmd = new SqlCommand();
- sqlCmd.CommandType = CommandType.Text;
- sqlCmd.CommandText = strSql;
- sqlCmd.Connection = sqlCon;
- sqlCon.Open();
- int i;
- i = sqlCmd.ExecuteNonQuery();
- sqlCon.Close();
- return i;
- }
- /// <summary>
- /// 执行传递过来的SQL语句
- /// </summary>
- /// <param name="strSql">string 要执行的SQL语句</param>
- /// <returns>int 受影响的行数</returns>
- public static object Static_excuteSqlCount(string strSql)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- //sql命令
- SqlCommand sqlCmd = new SqlCommand();
- sqlCmd.CommandType = CommandType.Text;
- sqlCmd.CommandText = strSql;
- sqlCmd.Connection = sqlCon;
- sqlCon.Open();
- object obj;
- obj = sqlCmd.ExecuteScalar();
- sqlCon.Close();
- return obj;
- }
- /// <summary>
- /// 执行存储过程返回数据集
- /// </summary>
- /// <param name="p_strProcName">存储过程名称</param>
- /// <param name="cmdParams">存储过程参数</param>
- /// <returns>Dataset</returns>
- public static DataSet Static_ExcuteQueryDataByProc(string p_strProcName, SqlParameter[] cmdParams)
- {
- //连接对象
- SqlConnection sqlCon = GetSqlconnection();
- DataSet dsTemp = new DataSet();
- SqlDataAdapter sqlAdapter = new SqlDataAdapter();
- SqlCommand sqlCmd = new SqlCommand();
- sqlCmd.CommandType = CommandType.StoredProcedure;
- sqlCmd.CommandText = p_strProcName;
- sqlCmd.Connection = sqlCon;
- //添加参数
- for (int i = 0; i < cmdParams.Length; i++)
- sqlCmd.Parameters.Add(cmdParams[i]);
- sqlAdapter.SelectCommand = sqlCmd;
- sqlAdapter.Fill(dsTemp);
- return dsTemp;
- }
- //
- }
【转】通用数据访问层--SQLServer版
最新推荐文章于 2024-08-20 20:45:00 发布