在使用ADO.NET经常用到SqlConnection、SqlCommand、SqlDataAdapter、DataSet等对象,可不可以把这些对象变成公共的,也就是说每个页面都可以直接调用,而不是每次都需要重新声明一次。我现在的想法是将它们封装到一个类,作为类的一个属性,希望各位兄弟能有更合适的办法,不吝赐教!谢谢!public class AdoNetCls{
  protected SqlConnection conn;
  protected SqlDataAdapter da;
  ...
  
  public SqlConnection Conn{
    get{
      return conn;
    }
    set{
      conn = value;
    }
  }  public SqlDataAdapter da{
  ...
  }  ...
}

解决方案 »

  1.   

    我是想看看大家是怎么做的,避免多次声明Ado.Net对象。有做过的朋友共享一下
      

  2.   

    可以写出单独的CS文件,名字空间在最上层。如
    DataCenter.cs
    namespace PROJE
    {
       public class DataCenter
    {
    ....其他页面:
    WebForms1.aspx.cs
    namespace PROJE.WebForms1
    {
       public class ExamineFile : System.Web.UI.Page
        {
    .....  DataCenter da=new DataCenter();
      

  3.   

    http://community.csdn.net/Expert/topic/4244/4244054.xml?temp=.434704这两个帖子的问题居然一样?奇怪了
      

  4.   

    很好的学习例子:http://w3cn.org/resource/down/2004/personalwebsite.rar
      

  5.   

    回复人: yeskele(混分的) ( ) 信誉:100 
    --------------------------------------
    对方注明了是引用我这贴的,呵呵,他比我有人气,我去看看他的贴子。
      

  6.   

    using System;
    using System.Data;
    using System.Data.SqlClient;namespace WuJian2005.DataAccess
    {
    /// <summary>
    /// 数据库操作基类
    /// </summary>

    //抽象类:不能实例化,只能继承,可以有抽象成员。
    public abstract class DBAccess
    {
    //数据库连接字符串
    private string connectionstring;
    //SqlConnection对象私有字段
    private SqlConnection conn;
    //SqlCommand对象
    private SqlCommand cmd = new SqlCommand();
    //DataAdapter对象
    private SqlDataAdapter da = new SqlDataAdapter();
    //DataSet对象
    private DataSet ds = new DataSet();
    //DataTable对象
    private DataTable dt = new DataTable();

    #region 构造函数
    public  DBAccess(string input_connectionstring)
    {
    connectionstring = input_connectionstring;
    conn = new SqlConnection(connectionstring);
    }
    #endregion #region *无参数存储过程创建SqlCommand对象
    public SqlCommand BuildSqlCommand (string procedure_name)
    {
    try
    {
    cmd = new SqlCommand(procedure_name , conn);
    cmd.CommandType = CommandType.StoredProcedure;
    return cmd;
    }
    catch (Exception error)
    {
    throw error;
    }
    }
    #endregion #region *有参数存储过程创建SqlCommand对象
    public SqlCommand BuildSqlCommand (string procedure_name , IDataParameter[] parameters)
    {
    try
    {
    cmd = new SqlCommand (procedure_name , conn);
    cmd.CommandType = CommandType.StoredProcedure;
    foreach (SqlParameter parameter in parameters)
    {
    cmd.Parameters.Add (parameter);
    }
    return cmd;
    }
    catch (Exception error)
    {
    throw error;
    }
    }
    #endregion #region 运行查询存储过程(例:SELECT COUNT(*) FROM table_name格式查询),返回行数
    public int RunSelectProcedure (string procedure_name , IDataParameter[] parameters)
    {
    int rows_count;
    try
    {
    if(conn.State == ConnectionState.Closed)
    {
    conn.Open();
    }
    cmd = this.BuildSqlCommand(procedure_name , parameters);
    rows_count = (int)cmd.ExecuteScalar();
    return rows_count;
    }
    catch(Exception error)
    {
    throw error;
    }
    finally
    {
    conn.Close();
    }
    }
    #endregion #region 运行存储过程(INSERT、UPDATE、DELETE),返回成功行数
    public int RunProcedure (string procedure_name , IDataParameter[] parameters)
    {
    int rows_count;
    try
    {
    if(conn.State == ConnectionState.Closed)
    {
    conn.Open();
    }
    cmd = this.BuildSqlCommand(procedure_name , parameters);
    rows_count = cmd.ExecuteNonQuery();
    return rows_count;
    }
    catch(Exception error)
    {
    throw error;
    }
    finally
    {
    conn.Close();
    }
    }
    #endregion #region 运行无参数存储过程(SELECT查询),返回一个DataTable
    public DataTable RunProcedure (string procedure_name , string table_name)
    {
    try
    {
    if(conn.State == ConnectionState.Closed)
    {
    conn.Open();
    }
    cmd = this.BuildSqlCommand(procedure_name);
    da = new SqlDataAdapter(cmd);
    da.Fill(ds,table_name);
    return ds.Tables[table_name];
    }
    catch(Exception error)
    {
    throw error;
    }
    finally
    {
    conn.Close();
    }
    }
    #endregion #region 为DataTable增加序号列(true:递增,false:递减),返回新的DataTable
    public DataTable AddNumber(DataTable table_object_name,string column_name,bool direction)
    {
    try
    {
    dt = table_object_name;
    //增加列
    dt.Columns.Add(column_name,System.Type.GetType("System.Int32"));
    if(direction == true)
    {
    //递增赋值
    for(int i=0;i<dt.Rows.Count;i++)
    {
    dt.Rows[i][column_name] = dt.Rows.Count-i;
    }
    return dt;
    }
    else
    {
    //递减赋值
    for(int i=dt.Rows.Count;i>0;i--)
    {
    dt.Rows[i-1][column_name] = i;
    }
    return dt;
    }
    }
    catch(Exception error)
    {
    throw error;
    }
    }
    #endregion }
    }