当前位置:编程学习 > C#/ASP.NET >>

这个asp.net数据库操作类好不好?

using System;
using System.Data;
using System.Data.SqlClient;
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;

/// <summary>
/// Index_DataBase 的摘要说明
/// </summary>
public class Index_DataBase
{
    private static Index_DataBase index_instance;
 public Index_DataBase()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    public static Index_DataBase Index_GetIntance()
    {
        if (index_instance == null)
        {
            index_instance = new Index_DataBase();
        }
        return index_instance;
    }
    public static SqlConnection Index_ReturnConn()
    {
        string strConn = System.Configuration.ConfigurationManager.AppSettings["Connection"];
        SqlConnection Conn=new SqlConnection(strConn);
        if (Conn.State.Equals(ConnectionState.Closed))
        {
            Conn.Open();
        }
        return Conn;
    }
    public static SqlCommand Index_CreateCmd(string procName,SqlParameter[] prams,SqlConnection Conn)
    {
        SqlConnection SqlConn = Conn;
        if (SqlConn.State.Equals(ConnectionState.Closed))
        {
            SqlConn.Open();
        }
        SqlCommand Cmd = new SqlCommand();
        Cmd.CommandType = CommandType.StoredProcedure;
        Cmd.Connection = SqlConn;
        Cmd.CommandText = procName;
        if (prams != null)
        {
            foreach (SqlParameter parameter in prams)
            {
                if (parameter != null)
                {
                    Cmd.Parameters.Add(parameter);
                }
            }
        }
        return Cmd;
    }
    public static SqlCommand Index_CreateCmd(string procName, SqlParameter[] prams)
    {
        SqlConnection Conn = Index_ReturnConn();
        SqlCommand Cmd = new SqlCommand();
        Cmd.CommandType = CommandType.StoredProcedure;
        Cmd.Connection = Conn;
        Cmd.CommandText = procName;
        if (prams != null)
        {
            foreach (SqlParameter parameter in prams)
            {
                if (parameter != null)
                {
                    Cmd.Parameters.Add(parameter);
                }
            }
        }
        return Cmd;
    }
    public static SqlCommand Index_CreateCmd(string procnName, SqlConnection Conn)
    {
        SqlConnection SqlConn = Conn;
        if (SqlConn.State.Equals(ConnectionState.Closed))
        {
            SqlConn.Open();
        }
        SqlCommand Cmd = new SqlCommand();
        Cmd.CommandType = CommandType.StoredProcedure;
        Cmd.Connection = SqlConn;
        Cmd.CommandText = procnName;
        return Cmd;
    }
    public static SqlCommand Index_CreateCmd(string procnName)
    {
        SqlConnection Conn = Index_ReturnConn();
        SqlCommand Cmd = new SqlCommand();
        Cmd.CommandType = CommandType.StoredProcedure;
        Cmd.Connection = Conn;
        Cmd.CommandText = procnName;
        return Cmd;
    }
    public static SqlDataReader Index_RunProcGetReader(string procName, SqlParameter[] prams)
    {
        SqlCommand Cmd = Index_CreateCmd(procName, prams);
        SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return Dr;
    }
    public static SqlDataReader Index_RunProcGetReader(string procName, SqlParameter[] prams,SqlConnection Conn)
    {
        SqlCommand Cmd = Index_CreateCmd(procName, prams, Conn);
        SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return Dr;
    }
    public static SqlDataReader Index_RunProcGetReader(string procName, SqlConnection Conn)
    {
        SqlCommand Cmd = Index_CreateCmd(procName, Conn);
        SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return Dr;
    }
    public static SqlDataReader Index_RunProcGetReader(string procName)
    {
        SqlCommand Cmd = Index_CreateCmd(procName);
        SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return Dr;
    }
    public static DataTable Index_RunProcGetTable(string procName, SqlParameter[] prams, SqlConnection Conn)
    {
        SqlCommand Cmd = Index_CreateCmd(procName,prams, Conn);
        SqlDataAdapter Dtr = new SqlDataAdapter();
        DataSet Ds = new DataSet();
        Dtr.SelectCommand = Cmd;
        Dtr.Fill(Ds);
        DataTable Dt = Ds.Tables[0];
        Conn.Close();
        return Dt;
    }
    public static int Index_RunExecute(string procName)
    {
        SqlConnection Conn = Index_ReturnConn();
        SqlCommand Cmd = Index_CreateCmd(procName, Conn);
        int intResult = Cmd.ExecuteNonQuery();
        Conn.Close();
        return intResult;
    }
    public static int Index_RunExecute(string procName, SqlParameter[] prams)
    {
        SqlConnection Conn = Index_ReturnConn();
        SqlCommand Cmd = Index_CreateCmd(procName,prams, Conn);
        int intResult = Cmd.ExecuteNonQuery();
        Conn.Close();
        return intResult;
    }
    public static int Index_RunExecuteScalar(string procName)
    {
        SqlConnection Conn = Index_ReturnConn();
        SqlCommand Cmd = Index_CreateCmd(procName, Conn);
        int intResult = Convert.ToInt32(Cmd.ExecuteScalar());
        Conn.Close();
        return intResult;
    }
    public static int Index_RunExecuteScalar(string procName, SqlParameter[] prams)
    {
        SqlConnection Conn = Index_ReturnConn();
        SqlCommand Cmd = Index_CreateCmd(procName, prams, Conn);
        int intResult = Convert.ToInt32(Cmd.ExecuteScalar());
        Conn.Close();
        return intResult;
    }
}

答案:using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace DAL
{
    public static class DBHelper
    {

        private static SqlConnection connection;
        public static SqlConnection Connection
        {
            get
            {
                string connectionString = "Data Source=.;Initial Catalog=MyUser;User ID=sa;password=sqlpass";   
               
                if (connection == null)//如果conn为空,就打开数据库
                {
                    connection = new SqlConnection(connectionString);
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Closed)//conn的状态是是关闭的,就打开数据库。
                {
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Broken)//如果链接中断,重新打开
                {
                    connection.Close();
                    connection.Open();
                }
                return connection;//返回一个连接对象
            }
        }

        public static int ExecuteCommand(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            int result = cmd.ExecuteNonQuery();
            return result;
        }

        public static int ExecuteCommand(string sql, SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteNonQuery();
        }

        public static int ExecuteCommand(string sql, SqlParameter value)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.Add(value);
            int result = cmd.ExecuteNonQuery();
            return result;
        }


        public static int ExecuteScalar(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            int result = (int)cmd.ExecuteScalar();
            return result;
        }

        public static int ExecuteScalar(string sql, SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            int result = (int)cmd.ExecuteScalar();
            return result;
        }

        public static int ExecuteScalar(string sql, SqlParameter value)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.Add(value);
            int result = (int)cmd.ExecuteScalar();
            return result;
        }

        public static SqlDataReader ExecuteReader(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }

        public static SqlDataReader ExecuteReader(string sql, SqlParameter value)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.Add(value);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }

        public static SqlDataReader ExecuteReader(string sql, SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
 &nb

上一个:asp.net到sql server 取值问题
下一个:ASP.NET如何在框架外面显示?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,