打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Dbconn 连接问题

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// DataAccess 的摘要说明。
/// <description>数据处理基类,调用方式:DataAccess.DataSet((string)sqlstr);或者DataAccess.DataSet((string)sqlstr,ref DataSet ds); </description>
/// </summary>
public class Dbconn
{
    #region 属性

    protected static SqlConnection conn = new SqlConnection();
    //protected static bool mustCloseConnection = false;
    //protected static SqlCommand comm = new SqlCommand();
    #endregion
    public Dbconn()
    {
        //init();
    }
    #region 内部函数 静态方法中不会执行Dbconn()构造函数

    /// <summary>
    /// 打开数据库连接
    /// </summary>
    private static void openConnection()
    {
        //SysConfig.ConnectionString 为系统配置类中连接字符串,如:"server=localhost;database=databasename;uid=sa;pwd=;"
        if (conn.State == ConnectionState.Closed)
        {
            conn.ConnectionString = ConfigurationManager.AppSettings["DBConnStr"]; //SysConfig.ConnectionString;
            conn.Open();
        }
        else
        {
            conn.Close();
            conn.ConnectionString = ConfigurationManager.AppSettings["DBConnStr"]; //SysConfig.ConnectionString;
            conn.Open();
        }
        //if (conn.State == ConnectionState.Closed)
        //{
        //    conn.ConnectionString = ConfigurationManager.AppSettings["DBConnStr"]; //SysConfig.ConnectionString;
        //    comm.Connection = conn;
        //    conn.Open();
        //}
        //else if (conn.State == ConnectionState.Broken)
        //{
        //    conn.ConnectionString = ConfigurationManager.AppSettings["DBConnStr"]; //SysConfig.ConnectionString;
        //    comm.Connection = conn;
        //    conn.Close();
        //    conn.Open();
        //}

    }
    /// <summary>
    /// 关闭当前数据库连接
    /// </summary>
    private static void closeConnection()
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
        conn.Dispose();
        //comm.Dispose();
    }
    #endregion
    /// <summary>
    /// 执行Sql查询语句
    /// </summary>
    /// <param >传入的Sql语句</param>
    public static void ExecuteSql(string sqlstr)
    {
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            comm.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Dispose();
            closeConnection();
        }
    }

    /// <summary>
    /// 执行存储过程
    /// </summary>
    /// <param >存储过程名</param>
    /// <param >SqlParameters 集合</param>
    public static void ExecutePorcedure(string procName, SqlParameter[] coll)
    {
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            for (int i = 0; i < coll.Length; i++)
            {
                comm.Parameters.Add(coll[i]);
            }
            comm.CommandType = CommandType.StoredProcedure;
            comm.CommandText = procName;
            comm.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Parameters.Clear();
            comm.Dispose();
            closeConnection();
        }
    }

    /// <summary>
    /// 执行存储过程并返回数据集
    /// </summary>
    /// <param >存储过程名称</param>
    /// <param >SqlParameter集合</param>
    /// <param >DataSet </param>
    public static void ExecutePorcedure(string procName, SqlParameter[] coll, ref DataSet ds)
    {
        SqlCommand comm = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        try
        {
           
            openConnection();
            comm.Connection = conn;
            for (int i = 0; i < coll.Length; i++)
            {
                comm.Parameters.Add(coll[i]);
            }
            comm.CommandType = CommandType.StoredProcedure;
            comm.CommandText = procName;
            comm.ExecuteNonQuery();
            da.SelectCommand = comm;
            da.Fill(ds);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Parameters.Clear();
            comm.Dispose();
            closeConnection();
        }
    }


    /// <summary>
    /// 执行Sql查询语句并返回第一行的第一条记录,返回值为object 使用时需要拆箱操作 -> Unbox
    /// </summary>
    /// <param >传入的Sql语句</param>
    /// <returns>object 返回值 </returns>
    public static object ExecuteScalar(string sqlstr)
    {
        object obj = new object();
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            obj = comm.ExecuteScalar();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Dispose();
            closeConnection();
        }
        return obj;
    }

    /// <summary>
    /// 执行Sql查询语句,同时进行事务处理
    /// </summary>
    /// <param >传入的Sql语句</param>
    public static void ExecuteSqlWithTransaction(string sqlstr)
    {
        SqlTransaction trans;
        trans = conn.BeginTransaction();
        SqlCommand comm = new SqlCommand();
        comm.Transaction = trans;
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            comm.ExecuteNonQuery();
            trans.Commit();
        }
        catch
        {
            trans.Rollback();
        }
        finally
        {
            comm.Dispose();
            closeConnection();
        }
    }

    /// <summary>
    /// 返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
    /// 方法关闭数据库连接
    /// </summary>
    /// <param >传入的Sql语句</param>
    /// <returns>SqlDataReader对象</returns>
    public static SqlDataReader dataReader(string sqlstr)
    {
        SqlDataReader dr = null;
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandText = sqlstr;
            comm.CommandType = CommandType.Text;
            dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
            comm.Dispose();
        }
        catch
        {
            try
            {
                if (dr != null && !dr.IsClosed)
                    dr.Close();
                closeConnection();
            }
            catch
            {
            }
        }
        return dr;
    }
    /// <summary>
    /// 返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
    /// 方法关闭数据库连接
    /// </summary>
    /// <param >传入的Sql语句</param>
    /// <param >传入的ref DataReader 对象</param>
    public static void dataReader(string sqlstr, ref SqlDataReader dr)
    {
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandText = sqlstr;
            comm.CommandType = CommandType.Text;
            dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch
        {
            try
            {
                if (dr != null && !dr.IsClosed)
                    dr.Close();
            }
            catch
            {
            }
            finally
            {
                comm.Dispose();
                closeConnection();
            }
        }
    }


    /// <summary>
    /// 返回指定Sql语句的DataSet
    /// </summary>
    /// <param >传入的Sql语句</param>
    /// <returns>DataSet</returns>
    public static DataSet dataSet(string sqlstr)
    {
        DataSet ds = new DataSet();
        SqlCommand comm = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Dispose();
            closeConnection();
        }
        return ds;
    }

    /// <summary>
    /// 返回指定Sql语句的DataSet
    /// </summary>
    /// <param >传入的Sql语句</param>
    /// <param >传入的引用DataSet对象</param>
    public static void dataSet(string sqlstr, ref DataSet ds)
    {
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Dispose();
            closeConnection();
        }
    }
    /// <summary>
    /// 返回指定Sql语句的DataTable
    /// </summary>
    /// <param >传入的Sql语句</param>
    /// <returns>DataTable</returns>
    public static DataTable dataTable(string sqlstr)
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable datatable = new DataTable();
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(datatable);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Dispose();
            closeConnection();
        }
        return datatable;
    }

    /// <summary>
    /// 执行带参数存储过程并返回数据集合
    /// </summary>
    /// <param >存储过程名称</param>
    /// <param >SqlParameterCollection 输入参数--此处改为SqlParameter对象(kxy 2006-11-11)</param>
    /// <returns></returns>
    public static DataTable dataTable(string procName, SqlParameter[] parameters)
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable datatable = new DataTable();
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.Parameters.Clear();
            comm.CommandType = CommandType.StoredProcedure;
            comm.CommandText = procName;
            foreach (SqlParameter para in parameters)
            {
                SqlParameter p = (SqlParameter)para;
                comm.Parameters.Add(p);
            }
            da.SelectCommand = comm;
            da.Fill(datatable);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Dispose();
            closeConnection();
        }
        return datatable;
    }

    /// <summary>
    /// 执行指定Sql语句,同时给传入DataTable进行赋值
    /// </summary>
    /// <param >传入的Sql语句</param>
    /// <param >ref DataTable dt </param>
    public static void dataTable(string sqlstr, ref DataTable dt)
    {
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(dt);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Dispose();
            closeConnection();
        }
    }

    public static DataView dataView(string sqlstr)
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataView dv = new DataView();
        DataSet ds = new DataSet();
        SqlCommand comm = new SqlCommand();
        try
        {
            openConnection();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
            dv = ds.Tables[0].DefaultView;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            comm.Dispose();
            closeConnection();
        }
        return dv;
    }

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Asp.net Access 数据访问通用类
asp.net数据库连接,三层架构方法调用
C#数据库连接
VS连接SQL Server数据库,增删改查详细教程(C#代码)
C#调用存储过程
.net执行存储过程 - 生活就是在不能坚持的时候继续坚持 - 博客园
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服