如何使用参数C#创建连接字符串

时间:2014-06-25 04:23:08

标签: c# string class connection layer

我是新手。遗憾!

我的Windows窗体应用程序有3层。 Presentation具有带有textEdit_Name和textEdit_Pass的Form_Login。

我的连接类:

public class _Connection
{
    public OleDbConnection GetConn(string _name, string _pass)
    {
        OleDbConnection _Conn = new OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID={1};Password={2};", @"C:\Test\Test.mdb", _name, _pass));
        return _Conn;
    }
}

我的数据访问层:

public class getDAL : IDisposable
{
    private _Connection getConn = new _Connection();
    OleDbConnection _Conn = new OleDbConnection();
    public DataTable getDatatable()
    {
        _Conn = getConn.GetConn();
        //Do something
    }
}

我怎样才能获得_Conn:

_name = textEdit_Name.Text and _pass = textEdit_Pass.Text

用户通过Form_Login

登录时

3 个答案:

答案 0 :(得分:1)

也许最简单的方法是在DAL中的静态类中设置ConnectionString细节。像这样:

public static class ConnectionDetails
{
    public static string UserName;
    public static string Password;

    public static string GetAccessConnectionString(string filepath)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", filepath);
        if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
            sb.Append("User Id=admin;Password=;");
        else
            sb.AppendFormat("User Id={0};Password={1};", UserName, Password);

        return sb.ToString();
    }
}

您的业务层需要设置UserName静态类的PasswordConnectionDetails字段,只要它们在您的表示层中发生更改,并且需要连接字符串的DAL类应该获取它们通过使用适当的文件路径调用ConnectionDetails.GetAccessConnectionString。或者将FilePath添加为该类中的静态字段之一,以便您可以从任何地方设置所有三个。

如果您打算使用多个数据库文件,那么键入文件名的连接数据字典可能是个好主意。

答案 1 :(得分:1)

您可以使用ConnectionStringBuilder类

public static class BuildConnection()
{    
    public static GetConnectionString(var userName, var Password)
    {
          OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
          builder.Driver = "Microsoft Access Driver (*.mdb)";        
          builder.Add("Dbq", @"C:\Test\Test.mdb");
          builder.Add("User Id", userName);
          builder.Add("Password", Password);
          return builder.ConnectionString; // Here is your connection String 
    }
}

答案 2 :(得分:0)

以下是如何将LoginForm TextBox .Text传递给业务层和数据访问层的示例。

数据访问层

class DALClass : IDisposable
{
    private _Connection getConn;
    OleDbConnection _Conn;

    public DALClass()
    {
         getConn = new _Connection();
    }

    public DataTable getDatatable(string sUserName, string sUserPass)
    {
        //pass on user name and password
        _Conn = getConn.GetConn(sUserName, sUserPass);
        //Do something

        return default(DataTable);  //return datatable 
    }
}

public class _Connection
{
    public OleDbConnection GetConn(string _name, string _pass)
    {
        OleDbConnection _Conn = new OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID={1};Password={2};", @"C:\Test\Test.mdb", _name, _pass));
        return _Conn;
    }
}

业务层

class BLClass
{
    DALClass _dal;       

    public BLClass()
    {
        _dal = new DALClass();            
    }

    public DataTable GetDataTable(string sUserName, string sUserPass)
    {
        return _dal.getDatatable(sUserName, sUserPass);
    }
}

登录表单

 public partial class LoginForm : Form
 {
    BLClass _bl;

    public LoginForm()
    {
        InitializeComponent();
        //object of business layer
        _bl = new BLClass();
    }

    private void button1_Click(object sender, EventArgs e)
    {    
        //pass text boxes values to Business Layer      
        DataTable dt = _bl.GetDataTable(textEdit_Name.Text, textEdit_Pass.Text);            
    }
 }