如何创建blConnection

时间:2015-07-08 16:14:07

标签: c# sql

如何创建blConnection(与数据库的连接)

1 个答案:

答案 0 :(得分:1)

这会将DB连接路径分配给变量

public class blConnection
{
    private string constr = "Data Source=.\\MSSQLEXPRESS;Initial Catalog=DB_Name;Integrated Security=True";

    public string getConstr()   
    {
        return constr;
    }
}

然后你可以在BL类中重用它

public class blCustomerInformation
{
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataAdapter ada;

    blConnection myConn = new blConnection();

    public blCustomerInformation()
    {
        conn = new SqlConnection(myConn.getConstr());
        conn.Open();

        cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        ada = new SqlDataAdapter();
        ada.SelectCommand = cmd;
    }
}