c# - How can I make the SQL connect in a Windows Form? -
i'm still working on messenger programme, more secure i'm testing stuff. i've got login form meant connect sql database, i've set local sql trying working (it still doesn't work) , wondering did wrong.
private void button1_click(object sender, eventargs e) { logininfo.un = textbox1.text; logininfo.pw = textbox2.text; sqlconnection con = new sqlconnection(); con.connectionstring = "data source=localhost:3306; user id=pxgamer;password=password1; initial catalog=login;"; try { con.open(); } catch (exception) { messagebox.show("error database connection"); } string qry1 = "select * login password=@password , username=@username"; sqlcommand com = new sqlcommand(qry1, con); com.parameters.addwithvalue("@username", logininfo.un); com.parameters.addwithvalue("@password", logininfo.pw); sqldatareader dr = com.executereader(); while (dr.read()) { if (dr.hasrows == true) { messagebox.show("login successful", "login information"); } } if (dr.hasrows == false) { messagebox.show("access denied", "login information"); } this.hide(); var frmmain = new frmmain(); frmmain.closed += (s, args) => this.close(); frmmain.show(); } so that's code, connection hangs @ first, , "error database connection" error appears. tried looking up, says it's when connection hasn't opened. i'm guessing it's wrong connection string.
in debugging, part gets highlighted:
sqldatareader dr = com.executereader(); this error shown:
an unhandled exception of type 'system.invalidoperationexception' occurred in system.data.dll
additional information: executereader requires open , available connection. connection's current state closed.
an unhandled exception of type 'system.data.sqlclient.sqlexception' occurred in system.data.dll
error without catch:
additional information: network-related or instance-specific error occurred while establishing connection sql server. server not found or not accessible. verify instance name correct , sql server configured allow remote connections. (provider: named pipes provider, error: 40 - not open connection sql server)
you trying connect mysql database using sql server-specific ado.net classes. won't work.
you can see current code trying connect sql server instance error message got:
additional information: network-related or instance-specific error occurred while establishing connection sql server. server not found or not accessible. verify instance name correct , sql server configured allow remote connections. (provider: named pipes provider, error: 40 - not open connection sql server)
you need download mysql-specific drivers connect mysql, , use instead.
Comments
Post a Comment