Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, December 23, 2019 6:26 PM
I am using VS 2019 community, wpf c# app and I created a database and table in the SQL Server Object explorer. Now I want to connect and query this table so what is the connection string I should use ?
SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;
string ConnectionString = "Data Source=(localdb)\MSSQLLocalDB;server=localhost;database=New Database";
con = new SqlConnection(ConnectionString);
con.Open();
All replies (7)
Monday, December 23, 2019 7:13 PM âś…Answered
Don't use Server Explorer. That is the old UI. Use SQL Server Object Explorer instead. Under SQL Server you'll find 2 instances of `(localdb)`. In most cases you'll be using the version that says `(localdb)\ProjectsV13` where `V13` is the version of the DB that is installed. Navigate to your database under `Databases`. Then right-click and select Properties. In the Properties window find `Connection String`. Copy that value and paste it into your config file. Note that it has a bunch more stuff than you need so you can clean it up if you need to. Otherwise you should now be using that database.
Michael Taylor http://www.michaeltaylorp3.net
Monday, December 23, 2019 7:08 PM | 1 vote
Here is a page to review, search for localdb as there are a few variations.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
Monday, December 23, 2019 7:31 PM
Thanks Michael I already using SQL Server Object Explorer. One question please how to open the console window to see my output ?
I need to see the output of this
Console.WriteLine("State: {0}", con.State);
Monday, December 23, 2019 7:45 PM | 1 vote
If your app is configured as a console app then when you debug it (F5) then the console window will open automatically. If you're running VS 2019 then it should stay open after debugging is done. If it doesn't then put a breakpoint on your last line of your Main function.
Michael Taylor http://www.michaeltaylorp3.net
Monday, December 23, 2019 10:23 PM
yes I am using VS 2019, project is wpf can I still open it or output to output window ?
Monday, December 23, 2019 10:32 PM | 1 vote
If you're building a WPF app then there is no console window. If you just want to see a debug message to the debugger then use `Debug.WriteLine` otherwise you'll have to "write" your output to whatever WPF window you are using for that sort of thing, if any.
Michael Taylor http://www.michaeltaylorp3.net
Tuesday, December 24, 2019 12:18 AM
Thanks alot