how to get connection string from app config in c#

后端 未结 2 1348
北荒
北荒 2020-12-22 14:27

My app.config file:


    
        

        
相关标签:
2条回答
  • 2020-12-22 15:07

    try

    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);
    

    It should work

    0 讨论(0)
  • 2020-12-22 15:18

    Basing my answer on this solution, try:

    conn.ConnectionString = ConfigurationManager.ConnectionStrings["MegaPixelBizConn"].ConnectionString;
    

    Make sure you have a reference to System.Configuration as well.

    Also remember to close your connection.

    EDIT

    Based on your new edit, try this for your code (Note I unfortunately can't test if this works due to my PC being broken).

    private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtUserName.Text == "" || txtPassword.Text == "")
                {
                    lblError.Visible = true;
                    lblError.Text = "*Enter UserName and Password";
                    //MessageBox.Show(" Enter UserName and Password .");
                    return;
                }
                else
                {
    
                    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MegaPixelBizConn"].ConnectionString))
                    {
    
                        con.Open();
    
                        using (SqlCommand cmd = new SqlCommand("SELECT * FROM login_info WHERE userName = @Username AND password = @Password", con))
                        {
                            // If these two lines don't work, replace "Username" and "Password" with "@Username"/"@Password"
                            cmd.Parameters.Add(new SqlParameter("Username", txtUserName.Text);
                            cmd.Parameters.Add(new SqlParameter("Password", txtPassword.Text);
                            SqlDataReader r = cmd.ExecuteReader();
                            if(r.HasRows())
                            {
                                // this assumes that there is only one user/password and no two users with same UID and PWORD
                                this.Hide();
                                Home hm = new Home();
                                hm.Show();
                            }
    
                            else
                            {
                                lblError.Text = "*Not Registered User or Invalid Name/Password";
                                //MessageBox.Show("Not Registered User or Invalid Name/Password");
                                txtPassword.Text = "";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
    
    
        }
    

    This will help prevent against SqlInjections as well.

    0 讨论(0)
提交回复
热议问题