Unknown Error Connecting To SQL Server in c# -


let me add quick explanation post:

i using visual studio , if create connection database works. can query database through database designer (the 1 see tables , can create new queries) , data processes correctly.

however using route getting same sql exception. me says in visual studio not set correctly wrong.

i using following code connect database on server (with using system.data.sqlclient; @ top):

sqlconnection thisconnection = new sqlconnection(); thisconnection.connectionstring =                                "data source=192.168.0.0,1433;" +                               "initial catalog=test-db;" +                               "user id=username;" +                               "password=password;"; thisconnection.open(); 

and getting following error:

at system.data.sqlclient.sqlconnection.onerror(sqlexception exception, tdsparserstate state)  @ system.data.sqlclient.sqlinternalconnection.onerror(sqlexception exception, tdsparserstate state)  @ system.data.sqlclient.tdsparser.throwexceptionandwarning()  @ system.data.sqlclient.tdsparser.connect(string host, sqlinternalconnection connhandler, int32 timeout)  @ system.data.sqlclient.sqlinternalconnection.openandlogin()  @ system.data.sqlclient.sqlinternalconnection..ctor(sqlconnection connection, hashtable connectionoptions)  @ system.data.sqlclient.sqlconnection.open()  @ inventorycontrol.login.validusername()  @ inventorycontrol.login.loginbutton_click(object sender, eventargs e)  @ system.windows.forms.control.onclick(eventargs e)  @ system.windows.forms.button.onclick(eventargs e)  @ system.windows.forms.buttonbase.wnproc(wm wm, int32 wparam, int32 lparam)  @ system.windows.forms.control._internalwnproc(wm wm, int32 wparam, int32 lparam)  @ microsoft.agl.forms.evl.entermainloop(intptr hwnmain)  @ system.windows.forms.application.run(form fm)  @ inventorycontrol.program.main() 

if have idea how resolve appreciated!

exception below:

system.data.sqlclient.sqlerror: sql server not exist or access denied 

however positive have permission server can access , query in visual studio.

i'm guessing can't find server. given default configuration on routers/modems, doubt 192.168.0.0 ip you're looking for. if it's on local machine, use localhost or 127.0.0.1 or computer name (e.g. hans-pc). if it's on machine, need make sure have right ip , configured remote access in both firewall , sql server configuration manager (enable tcp/ip and/or named pipes protocols).

also, can use few aliases properties in string shorten up:

server=192.168.0.0;database=test-db;user id=username;password=password                                                                       ↑                                       make sure no trailing semicolon ┘ 

i dropped port 1433 default port sql server.

to more information exception, try code:

try {    ...    thisconnection.open();    ... } catch (sqlexception ex) {     (int = 0; < ex.errors.count; i++)     {         console.writeline(ex.errors[i].tostring());         // or output them wherever need see them     } } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -