简体   繁体   English

使用C#以编程方式创建SQL Server数据库

[英]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". 本教程本身在步骤5中告诉我“将连接字符串更改为指向运行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. 我不知道我的SQL Server是否正在运行,如果是,我不知道服务器的名称是什么。 I know I installed SQL Server when I installed VS 2010 (I did a full installation), so it should be somewhere. 我知道我在安装VS 2010时安装了SQL Server(我做了一个完整的安装),所以它应该在某个地方。 I haven't changed anything in the SQL Server configuration so everything should be on what is default. 我没有更改SQL Server配置中的任何内容,所以一切都应该是默认的。

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: 如果您将SQL Server与VS 2010一起安装,则您的计算机上安装了SQL Server 2008 Express版本,默认情况下它安装为“SQLExpress”实例,因此您的连接字符串必须类似于:

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 如果要以编程方式创建数据库,则必须连接到master数据库

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

and then run a SQL statement something like this from your app: 然后从您的应用程序运行类似这样的SQL语句:

CREATE DATABASE (newDatabaseName)

You can check under services (start/run/services.msc) whether your SQL server is running or not; 您可以在服务(start / run / services.msc)下检查您的SQL服务器是否正在运行; or in your start menu under "sql server configuration manager". 或者在“sql server configuration manager”下的开始菜单中。 pls check if named pipes and tcp-ip are enabled. 请检查是否启用了命名管道和tcp-ip。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中以编程方式创建SQL Server数据库 - How to create a SQL Server database programmatically in C# C#-以编程方式创建SQL Server表 - C# - Create SQL Server table programmatically 如何在 C# 中以编程方式创建 SQL Server 数据库 - 为每个项目创建新 SQL Server 数据库的项目管理应用程序 - How to create SQL Server database programmatically in C# - Project management application that creates a new SQL Server database for each project 如何从C#以编程方式创建SQL Server数据库文件(.mdf)? - How can I create a SQL Server database file (.mdf) programmatically from C#? 以编程方式创建SQL Server CE 4.0数据库并在C#中添加架构? - Programmatically create SQL Server CE 4.0 Database and add a schema in C#? 如何以编程方式检查SQL数据库/服务器是否支持C#压缩? - How to programmatically check if a SQL database/server supports compression in C#? 是否可以以C#在SQL Server 2005中以编程方式创建DBLink? - Is it possible to programmatically create a DBLink in SQL server 2005 in C#? 有没有办法以编程方式从 C# 语句(如 SQL 服务器上的 CREATE/ALTER DATABASE)调用而不会产生安全问题? - Is there a way to programmatically call from C# statements like CREATE/ALTER DATABASE on SQL Server without creating security issues? 以编程方式创建SQL Server CE数据库文件 - Create SQL Server CE database file programmatically 在C#中以编程方式从数据库创建XSD - Create XSD from database programmatically in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM