C#访问数据库的基类

来源:互联网 发布:部队网络安全保密教案 编辑:程序博客网 时间:2024/05/16 17:59

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>
/// Class1 的摘要说明
/// </summary>
public class DataAccess
{
    #region 申明私有变量;
    private SqlConnection sqlConn;
    #endregion

    #region 构造函数
    public DataAccess(string connectionString)
    {
        if (sqlConn == null)
        {
            sqlConn = new SqlConnection(connectionString);
        }
    }

    public DataAccess()
    {
        string connectionString = System.Configuration.ConfigurationSettings.AppSettings["SqlConn"];
        if (sqlConn == null)
        {
            sqlConn = new SqlConnection(connectionString);
        }
    }
    #endregion

    #region 生成SqlCommand
    private SqlCommand BuildCommand(string sen, CommandType Type)
    {
        SqlCommand command = new SqlCommand();
        command.Connection = sqlConn;
        command.CommandText = sen;
        command.CommandType = Type;
        return command;
    }
    private SqlCommand BuildCommand(string sen, CommandType Type, SqlParameter[] parameters)
    {
        SqlCommand command = new SqlCommand();

        command.Connection = sqlConn;
        command.CommandText = sen;
        command.CommandType = Type;

        foreach (SqlParameter parameter in parameters)
        {
            command.Parameters.Add(parameter);
        }
        return command;
    }
    public SqlCommand BuildQueryCommand(string ProcedureName, IDataParameter[] parameters)
    {
        SqlCommand command = new SqlCommand(ProcedureName, sqlConn);
        command.CommandType = CommandType.StoredProcedure;
        foreach (SqlParameter parameter in parameters)
        {
            command.Parameters.Add(parameter);
        }
        return command;
    }
    #endregion

    #region  运行存储过程,对数据表进行操作
    public void RunProcedure(string ProcedureName)
    {
        SqlCommand command = BuildCommand(ProcedureName, CommandType.StoredProcedure);

        sqlConn.Open();
        command.ExecuteNonQuery();
        sqlConn.Close();

        command.Dispose();
    }
    public void RunProcedure(string ProcedureName, SqlParameter[] parameters)
    {
        SqlCommand command = BuildCommand(ProcedureName, CommandType.StoredProcedure, parameters);

        sqlConn.Open();
        command.ExecuteNonQuery();
        sqlConn.Close();

        command.Dispose();
    }

    public SqlDataReader RunProcedureReader(string ProcedureName, IDataParameter[] parameters)
    {
        SqlDataReader returnReader;

        sqlConn.Open();
        SqlCommand command = BuildQueryCommand(ProcedureName, parameters);
        command.CommandType = CommandType.StoredProcedure;

        returnReader = command.ExecuteReader();
        return returnReader;
    }

    public int RunProcedureToValue(string ProcedureName)
    {
        SqlCommand command = BuildCommand(ProcedureName, CommandType.StoredProcedure);

        command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue,
            false, 0, 0, string.Empty, DataRowVersion.Default, null));

        sqlConn.Open();
        command.ExecuteNonQuery();
        int result = (int)command.Parameters["ReturnValue"].Value;
        sqlConn.Close();

        command.Dispose();
        return result;
    }
    public int RunProcedureToValue(string ProcedureName, SqlParameter[] parameters)
    {
        SqlCommand command = BuildCommand(ProcedureName, CommandType.StoredProcedure, parameters);

        command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue,
            false, 0, 0, string.Empty, DataRowVersion.Default, null));

        sqlConn.Open();
        command.ExecuteNonQuery();
        int result = (int)command.Parameters["ReturnValue"].Value;
        sqlConn.Close();

        command.Dispose();

        return result;
    }

    public DataSet RunProcedureToDs(string ProcedureName)
    {
        DataSet ds = new DataSet();
        SqlCommand command = BuildCommand(ProcedureName, CommandType.StoredProcedure);
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);

        sqlConn.Open();
        sqlDataAdapter.Fill(ds);
        sqlConn.Close();

        command.Dispose();
        sqlDataAdapter.Dispose();

        return ds;
    }
    public DataSet RunProcedureToDs(string ProcedureName, SqlParameter[] parameters)
    {
        DataSet ds = new DataSet();
        SqlCommand command = BuildCommand(ProcedureName, CommandType.StoredProcedure, parameters);
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);

        sqlConn.Open();
        sqlDataAdapter.Fill(ds);
        sqlConn.Close();

        command.Dispose();
        sqlDataAdapter.Dispose();

        return ds;
    }

    public DataTable RunProcedureToDt(string ProcedureName)
    {
        DataTable dt = new DataTable();
        SqlCommand command = BuildCommand(ProcedureName, CommandType.StoredProcedure);
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);

        sqlConn.Open();
        sqlDataAdapter.Fill(dt);
        sqlConn.Close();

        command.Dispose();
        sqlDataAdapter.Dispose();

        return dt;
    }
    public DataTable RunProcedureToDt(string ProcedureName, SqlParameter[] parameters)
    {
        DataTable dt = new DataTable();
        SqlCommand command = BuildCommand(ProcedureName, CommandType.StoredProcedure, parameters);
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);

        sqlConn.Open();
        sqlDataAdapter.Fill(dt);
        sqlConn.Close();

        command.Dispose();
        sqlDataAdapter.Dispose();

        return dt;
    }
    #endregion

    #region  运行SQL语句,对数据表进行操作
    public void RunSql(string Sqlsen)
    {
        SqlCommand command = BuildCommand(Sqlsen, CommandType.Text);
        command.Connection = sqlConn;

        sqlConn.Open();
        command.ExecuteNonQuery();
        sqlConn.Close();

        command.Dispose();
    }
    public DataSet RunSqlToDs(string Sqlsen)
    {
        SqlCommand command = BuildCommand(Sqlsen, CommandType.Text);
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
        DataSet ds = new DataSet();

        command.Connection = sqlConn;
        sqlDataAdapter.SelectCommand = command;

        sqlConn.Open();
        sqlDataAdapter.Fill(ds);
        sqlConn.Close();

        command.Dispose();
        sqlDataAdapter.Dispose();

        return ds;
    }
    #endregion
}
 

原创粉丝点击