简体   繁体   English

使用SQL脚本创建SQL Server数据库

[英]use SQL script to create SQL server DB

I have a very basic question about SQL server and Visual Studio 2010. I'm currently reading the book "C# 3.0 Unleashed with the .NET framework 3.5". 我对SQL Server和Visual Studio 2010有一个非常基本的问题。我目前正在阅读《 C#3.0与.NET Framework 3.5一起发布》一书。 I'm at the chapter where LINQ to SQL is explained. 我在解释LINQ to SQL的章节中。 The writer asks to create a new database and use his script (given in the book) to create the tables and fill these with some data. 作者要求创建一个新的数据库,并使用他的脚本(在书中给出)来创建表并用一些数据填充这些表。

However, as I've never worked with SQL server before, this part is a bit confusing for me. 但是,由于我以前从未使用过SQL Server,因此这部分令我有些困惑。 The writer doesn't explain how to create the database. 作者没有解释如何创建数据库。 First I thought I could just right-click on Data Connections -> Create New SQL Server Database . 首先,我认为我可以右键单击Data Connections -> Create New SQL Server Database However, when I tried that, I only got an error explaining no connection could be made. 但是,当我尝试这样做时,我只得到一条错误消息,说明无法建立连接。

When that didn't work, I added a new Service-based Database via Add -> New Item . 当那行不通时,我通过Add -> New Item添加了一个新Service-based Database This works (although I do not understand the difference between this and the previous thing I've tried. If someone could explain this for me, It'd be very appreciated), but now I'm supposed to use the script to create the tables and enter some data. 这项工作有效(尽管我不理解这与我之前尝试过的事情之间的区别。如果有人可以为我解释一下,将不胜感激),但是现在我应该使用脚本来创建表并输入一些数据。 I can right-click on the database and select New Query , but that window doesn't seem to accept such scripts. 我可以右键单击数据库并选择New Query ,但是该窗口似乎不接受此类脚本。

First part of the script: 脚本的第一部分:

/****** Object: Table [dbo].[HospitalStaff]Script Date: 12/29/2007 21:42:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (
SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N’[HospitalStaff]’)
AND type in (N’U’))
BEGIN
CREATE TABLE [HospitalStaff](
[HospitalStaffID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50)
COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Position] [varchar](50)
COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
CONSTRAINT [PK_HospitalStaff] PRIMARY KEY CLUSTERED
(
[HospitalStaffID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON)
)
END

I know it's a very basic question, but where can I execute this script? 我知道这是一个非常基本的问题,但是我可以在哪里执行此脚本? I've visited several websites with SQL server tutorials, but it's all pretty confusing. 我已经访问了一些具有SQL Server教程的网站,但这一切都令人困惑。

  • Add a new .sql file in your solution with .sql extension 在您的解决方案中以.sql扩展名添加新的.sql文件
  • Open that file 打开那个文件
  • Right click 右键点击
  • Execute script 执行脚本

在此处输入图片说明

more info at 有关更多信息,请访问

http://msdn.microsoft.com/en-us/library/yea4bc1b(vs.80).aspx http://msdn.microsoft.com/zh-CN/library/yea4bc1b(vs.80).aspx

You may use a program to access it. 您可以使用程序来访问它。

For instance SQL Server Management Studio is an integrated environment for accessing, configuring, managing, administering, and developing all components of SQL Server. 例如,SQL Server Management Studio是一个用于访问,配置,管理,管理和开发SQL Server所有组件的集成环境。

Check more info HERE . 此处查看更多信息。

Found an eloquent solution 找到了雄辩的解决方案

Problem Database in Asp.NET web Visual Studio 2010 when running on PC with SQL Server 2008 Express I can use the interface to manually add tables but I wanted to run SQL code to create tables etc. You can't do this from the query interface ( highlight database and from popup choose "New Query") 在带有SQL Server 2008 Express的PC上运行时,Asp.NET Web Visual Studio 2010中的问题数据库我可以使用该界面手动添加表,但是我想运行SQL代码来创建表等。您无法从查询界面执行此操作( 突出显示数据库,然后从弹出菜单中选择“新建查询”)

The idea stated above of using a script seemed like the way to go – but when I went to do that I couldn't figure out how to connect to my database that was in the solution. 上面提到的使用脚本的想法似乎很可行–但是当我这样做时,我无法弄清楚如何连接到解决方案中的数据库。

If I choose “Connection…” and the SQL Server Express 2008 that was running on my PC it would default to “master” and when I tried to browse to the database using [Options] it wasn't in the list. 如果我选择“连接…”,并且在我的PC上运行的SQL Server Express 2008将默认为“主”,而当我尝试使用[选项]浏览到数据库时,它不在列表中。 It also went to “master” when I explicitly put the full path in C:\\Users\\keef.riffhard\\Documents\\Visual Studio 2010\\WebSites\\WebSite1\\App_Data\\MyAspDatabase.mdf 当我将完整路径明确放在C:\\ Users \\ keef.riffhard \\ Documents \\ Visual Studio 2010 \\ WebSites \\ WebSite1 \\ App_Data \\ MyAspDatabase.mdf中时,它也变为“主”

AGGGG!! 啊!

The trick was also in [Options] is “Additional Connection Parameters” with a note “Connection string parameters override graphical selections in other panels” So I highlighted the database in my solution and then went to properties and copied the connection string. [选项]中的技巧还在于“其他连接参数”,并带有注释“连接字符串参数将覆盖其他面板中的图形选择”。因此,我在解决方案中突出显示了数据库,然后转到属性并复制了连接字符串。 I then pasted the connection string into the |Additional Connection Parameters| 然后,我将连接字符串粘贴到|其他连接参数|中。 tab under [Options] [选项]下的标签

SUCCESS !! 成功!

Right click on a database name in the Object Explorer in SSMS, go to Tasks -> Generate Scripts for all objects. 右键单击SSMS中“对象资源管理器”中的数据库名称,然后转到“任务”->“为所有对象生成脚本”。 Save it to a new query window, and you can see the text necessary for creating a new database from scratch and populating it with objects (tables, SPs, etc.) 将其保存到新的查询窗口,您将看到从头创建新数据库并用对象(表,SP等)填充新数据库所必需的文本。

Use this code to help you figure out how to generate a create string. 使用此代码可帮助您弄清楚如何生成创建字符串。

Example: 例:

USE [master]
GO
/****** Object:  Database [DBA]    Script Date: 02/07/2012 11:40:09 ******/
CREATE DATABASE [DBA] ON  PRIMARY 
( NAME = N'DBA', FILENAME = N'D:\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DBA.mdf' , SIZE = 1303552KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10240KB )
 LOG ON 
( NAME = N'DBA_log', FILENAME = N'D:\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DBA_log.ldf' , SIZE = 2052672KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
...

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

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