To:hillfree(比较土,请原谅) 就是你说的那种,可是怎么做呢?

解决方案 »

  1.   

    可以把他們封裝到類甚至命名空間,
    再行調用.
    namespace mine.data{
       public class connectdata{
             public void getconn(string connstr){
                    ...
             }
       }
    }
      

  2.   

    To:
    To:misfans(fans) 
    你说的这样只能将变量放入,但是无法使用创建好的实例的
      

  3.   

    建立一个域用来保存一打开的连接,
    如果你想省事的话,直接public这个域,
    如果要规范一点的话,就用一个属性来返回这个域的值。
      

  4.   

    如果你是在WINDOWS应用中,楼上的已经解决,如果你是在WEB应用中,hbxtx(xy)已经解决。
    对于WEB应用的解决,可以在Page_Load中完成:
    定义连接域conn,然后在Page_Load中:
    if(IsPostBack)
        this.conn=(conn类型名)Session["conn"];
    else
    {
        this.conn=new conn类型名(连接串);
        this.conn.Open();
        Session["conn"]=this.conn;
    }
      

  5.   

    作一个dll好了,让web和winform都能用的
      

  6.   

    >>To:
    >>To:misfans(fans) 
    >>你说的这样只能将变量放入,但是无法使用创建好的实例的
    我可是用了好久了哦!而且很方便的,省了我好多代碼.
    see:
    ----------------------------------------------------------
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; /* Usage:
     * using fans.datacommand;
     * public class xx{
     * public void xxx(){
     * MM_DataCommand x = new MM_DataCommand(["server=xxxx;database=xx;uid=xx;pwd=xx"]);
     * DataSet ds = x.getds;
     * if(ds==null)
     * return x.voidmsg
     * }
     * }
     * ......
     * 2002/06/20 ---Fans
     * */namespace fans.datacommand
    {
    /// <summary>
    /// 對sql server資料庫讀取及操作的模組.
    /// </summary>
    public class MM_DataCommand
    {
    public string connstr = "server=172.18.17.168;database=fans;uid=fans;pwd=xxxx";
    public SqlConnection myconn;
    public SqlDataAdapter myada;
    public SqlCommand mycomm;
    public DataSet myds;
    public SqlDataReader mydr;
    public string voidmsg = null;
    //this variable used to show messages when error occured in void functions.
    //developers can get error messages throught show this.value. public MM_DataCommand(string connstr)
    {
    //
    // if connection string needed be changed!
    //
    this.connstr = connstr;
    myconn = new SqlConnection(this.connstr);
    }

    public MM_DataCommand(){
    myconn = new SqlConnection(this.connstr);
    } public void datacommand(string datastr){
    try{
    mycomm = new SqlCommand(datastr,myconn);
    mycomm.Connection.Open();
    mycomm.ExecuteNonQuery();
    }
    catch(Exception exc){
    voidmsg = exc.Message;
    }
    finally{
    myconn.Close();
    }
    } public void opdata(){
    myconn = new SqlConnection(this.connstr);
    } public bool connisopen(){
    if(myconn.State.ToString() == "Closed")
    return false;
    else
    return true;
    } public DataSet getds(string sqlstr,string tbname){
    try{myada = new SqlDataAdapter(sqlstr,myconn);
    myds = new DataSet();
    myada.Fill(myds,tbname);
    }
    catch(Exception exc){
    voidmsg = exc.Message;
    }
    return myds;
    } public SqlDataReader getread(string readsqlstr){
    try{
    mycomm = new SqlCommand(readsqlstr,myconn);
    mydr = mycomm.ExecuteReader();
    }
    catch(Exception exc){
    voidmsg = exc.Message;
    }
    return mydr;
    } public void binddatagrid(string sqlstr,string tbname,DataGrid dl){
    //tbname為儲存到recordset里的資料表名.
    //為規范起見,請使用网頁名稱以使不同.
    if(!connisopen())
    opdata();
    myds = getds(sqlstr,tbname);
    dl.DataSource = myds.Tables[tbname].DefaultView;
    dl.DataBind();
    myconn.Close();  //remember to close this connection!
    } public void binddatalist(string sqlstr,string tbname,DataList dl){
    if(!connisopen())
    opdata();
    myds = getds(sqlstr,tbname);
    dl.DataSource = myds.Tables[tbname].DefaultView;
    dl.DataBind();
    myconn.Close();
    } public void bindrepeat(string sqlstr,string tbname,Repeater rt){
    if(!connisopen())
    opdata();
    myds = getds(sqlstr,tbname);
    rt.DataSource = myds.Tables[tbname].DefaultView;
    rt.DataBind();
    myconn.Close();
    } public void binddropdownlist(string sqlstr,string tbname,DropDownList ddl,string valuefield,string textfield){
    if(!connisopen())
    opdata();
    myds = getds(sqlstr,tbname);
    ddl.DataSource = myds.Tables[tbname].DefaultView;
    ddl.DataTextField = textfield;
    ddl.DataValueField = valuefield;
    ddl.DataBind();
    myconn.Close();
    }
    }
    }