sql - Unable to connect to a Local DB (mdf) from C# form application -


i tried play database creation , queries. in order started c# form application, added database service added table values, wanted use code retrieve values. here's code:

string conn = "data source = ./sqlexpress; attachdbfilename=c:\\users\\asus\\desktop\\robamia\\sqlserver\\windowsformsapplication3\\windowsformsapplication3\\database1.mdf; integrated security=true;connect timeout=30;user instance=true";  sqlconnection sql = new sqlconnection(conn);  sql.open(); messagebox.show("connection opened"); sql.close(); 

sadly program throws exception when comes open because seems cannot find database...

"server not found or not accessible"

i don't know problem, suggest?


ok, seems work incorrect syntax query

string conn = "server=(localdb)\v11.0; attachdbfilename=c:\users\asus\desktop\robamia\sqlserver\windowsformsapplication3\windowsformsapplication3\database1.mdf; integrated security=true;connect timeout=30;user instance=false";

        string querystring = "select * table";         sqlconnection sql = new sqlconnection(conn);          sql.open();         sqldataadapter adapter = new sqldataadapter();         sqlcommand command = new sqlcommand(querystring, sql);        /* --->here error*/ command.executenonquery();          dataset data = new dataset();         adapter.fill(data);         messagebox.show(data.tostring());         sql.close(); 

it looks data source part connection string wrong. should be:

"data source=.\\sqlexpress" 

complete:

string conn = "data source=.\\sqlexpress; attachdbfilename=c:\\users\\asus\\desktop\\robamia\\sqlserver\\windowsformsapplication3\\windowsformsapplication3\\database1.mdf; integrated security=true;connect timeout=30;user instance=true"; 

https://www.connectionstrings.com/sql-server/

as additional note, may best off placing in app.config or web.config file encase reference connection string multiple times , later decide change value of it.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -