简体   繁体   中英

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.

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)

You can check under services (start/run/services.msc) whether your SQL server is running or not; or in your start menu under "sql server configuration manager". pls check if named pipes and tcp-ip are enabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM