本文介绍了如何从SQLHelper访问此参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道如何访问此参数 _dbConn .因此,我不需要仅在SQLHelper中就在项目上创建字符串连接,当我使用它时,它会自动打开连接.

Hi,

I want to know how to access this parameter _dbConn. So I dont need to create a string connection on the project, just only in the SQLHelper and when I use it, it automatically open the connection.

public SQLHelper()
{
_dbConn = new SqlConnection("Data Source=User\\SQLEXPRESS;" +"Trusted_Connection=yes;database=myfirstdatabase;" +"MultipleActiveResultSets=True;Connection Timeout=15");
_dbConn.Open();
}



谢谢!



Thanks!

推荐答案

namespace DBHelper
{
    public sealed class DB
    {
        const string MDBConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Jet OLEDB:Database Password=imdad";
        private DB()
        {
        }
        public static DataTable getDataTable(string strsql)
        {
            OleDbConnection con = new OleDbConnection();
            con = getConnection();
            OleDbDataAdapter da = new OleDbDataAdapter(strsql, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }
        public static OleDbConnection getConnection()
        {
            OleDbConnection con = new OleDbConnection();
            string MDBFilePath = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\SBSysDB.mdb";
            string strcon = MDBConnection.Replace("{0}", MDBFilePath);
            con = new OleDbConnection(strcon);
            if (con.State != ConnectionState.Open) con.Open();
            return con;
        }
        public static void closeConnection(OleDbConnection con)
        {
            if (con.State == ConnectionState.Open) con.Close();
        }
    }
}


3.以表格形式访问该方法


3. Access the method in forms

string strsql = "SELECT HallID,Code,Name FROM Hall ORDER BY Name";
DataTable dtcategory = null;
dtcategory = DBHelper.DB.getDataTable(strsql);



如有任何疑问,请让我知道.

如果对您有帮助,请提供投票.

谢谢,
Imdadhusen



Please do let me know, if you have any doubt.

Please provide Vote if this would be helpful to you.

Thanks,
Imdadhusen


这篇关于如何从SQLHelper访问此参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:33