sql - Unable to connect to database C# -
i've been trying connect ms sql database through connection string in app.config, reason fails login, can't seem figure out.
this connection method:
public void con() { string username = usernamebox.text; string password = passwordbox.text; bool loginfail; sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["lagerconn"].connectionstring); //search connstring user id= & password= , replace username , password textboxes if (_connstring.contains("user id=")) { _connstring = _connstring.replace("user id=;", "user id=" + username + ";"); } if (_connstring.contains("password=")) { _connstring = _connstring.replace("password=", "password='" + password + "'"); } try { conn.open(); conn.close(); loginfail = false; }catch { messagebox.show("login failed"); loginfail = true; } if(loginfail == false) //if login successful change next form , hide connect form { mainmenu secondform = new mainmenu(); secondform.show(); this.hide(); } }`
and here app.config
<add name="lagerconn" connectionstring="data source=lagerserver;initial catalog=lagerdb;persist security info=true;user id=;password=" providername="system.data.sqlclient" /> </connectionstrings>
there's no point putting empty user id , password field in connection string @ all. leave them out altogether , rid of pointless persist security info
too. use connection string builder, e.g.
sqlconnection builder = new sqlconnectionstringbuilder(configurationmanager.connectionstrings["lagerconn"].connectionstring); builder.userid = userid; builder.password = password; sqlconnection connection = new sqlconnection(builder.connectionstring);
after that, don't provide generic error message , ignore information system gives you. @ exception , tell went wrong.
Comments
Post a Comment