Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create SQL Server database programmatically with C#

I'm trying to follow this example but I get an Exception related to the connection string telling me that the server was not found or was not accessible. The tutorial itself tells me on step 5 to "Change the connection string to point to your computer running SQL Server". I don't know if my SQL Server is running or not and if it is I don't know what would be the name of the server. I know I installed SQL Server when I installed VS 2010 (I did a full installation), so it should be somewhere. I haven't changed anything in the SQL Server configuration so everything should be on what is default.

like image 683
Juan Avatar asked Feb 27 '26 20:02

Juan


1 Answers

If you installed SQL Server along with VS 2010, you have SQL Server 2008 Express edition on your machine, and it is installed by default as a "SQLExpress" instance, so your connection string would have to be something like:

server=(local)\SQLExpress;database=(whatever_you_want);integrated security=SSPI;

This would expect that database you specify to already exist on your server.

If you want to programmatically create a database, you would have to connect to the master database

server=(local)\SQLExpress;database=master;integrated security=SSPI;

and then run a SQL statement something like this from your app:

CREATE DATABASE (newDatabaseName)
like image 155
marc_s Avatar answered Mar 01 '26 08:03

marc_s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!