首页 > 解决方案 > 如何使用 Bltoolkit 在运行时更改连接字符串

问题描述

我正在使用“Bltoolkit”来访问 sql server 数据。它按照我的要求工作正常。现在我正在尝试在运行时更改连接字符串。我尝试了下面的代码,但连接没有改变,这意味着它显示了默认连接字符串.

protected void LogIn_Clicked(object sender, EventArgs e)
    {
        DataTable Login = new DataTable();

        string username = Request.Form["username"];
        string password = Request.Form["password"];
        bool remember = RememberMe.Checked;

        DbManager.AddConnectionString(ConfigurationManager.ConnectionStrings["Default"].ConnectionString); // Default Connection String


        using (DbManager Db = new DbManager())
        {
            Login = Db.SetCommand("Select * from UserList Where UserName = '" + username + "' And Password = '" + password + "' ").ExecuteDataTable();

            if (Login.Rows.Count > 0)
            {
                Session["UserId"] = Login.Rows[0]["UserId"];
                Session["UserName"] = Login.Rows[0]["UserName"];
                Session["Division"] = Login.Rows[0]["Division"];
                Db.SetCommand("UpDate UserList Set LoginTime = '" + Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd HH:mm") + "' Where UserId = " + Convert.ToInt16(Login.Rows[0]["UserId"]) + " ").ExecuteScalar();
                DbManager.AddConnectionString(ConfigurationManager.AppSettings["GLD"]); // Changed Connection string from Web.Config File
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                ErrorMessage.Text = "Invalid UserName or Password";
                ErrorMessage.Visible = true;
            }
            Db.Close();
            Db.Dispose();
        }
    }

标签: c#bltoolkit

解决方案


推荐阅读