0

我正在尝试使用 C# 从我拥有的 sql 文件中恢复空白数据库的表。

这是代码:

namespace KezberProjectManager
{
    public partial class VisitClear : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sqlConnectionString = "Data Source=JOSH-PC\\SQLEXPRESS;Initial Catalog=KezBlu;Integrated Security=True;MultipleActiveResultSets=True";
            FileInfo file = new FileInfo(Path.Combine(
    HostingEnvironment.ApplicationPhysicalPath, 
    @"App_Data\kb.sql"));

            string script = file.OpenText().ReadToEnd();

            SqlConnection conn = new SqlConnection(sqlConnectionString);

            Server server = new Server(new ServerConnection(conn));

            server.ConnectionContext.ExecuteNonQuery(script);

        }
    }
}

这是脚本:

USE [KezBlu]
GO
/****** Object:  ForeignKey [Customer_Case]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Cases] DROP CONSTRAINT [Customer_Case]
GO
/****** Object:  ForeignKey [Schedule_Case]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Cases] DROP CONSTRAINT [Schedule_Case]
GO
/****** Object:  ForeignKey [Status_Case]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Cases] DROP CONSTRAINT [Status_Case]
GO
/****** Object:  ForeignKey [Group_Employee]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Employee] DROP CONSTRAINT [Group_Employee]
GO
/****** Object:  ForeignKey [Employee_EmployeGroupFilter]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[EmployeGroupFilters] DROP CONSTRAINT [Employee_EmployeGroupFilter]
GO
/****** Object:  ForeignKey [Group_EmployeGroupFilter]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[EmployeGroupFilters] DROP CONSTRAINT [Group_EmployeGroupFilter]
GO
/****** Object:  ForeignKey [Employee_LockedDate]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[LockedDates] DROP CONSTRAINT [Employee_LockedDate]
GO
/****** Object:  ForeignKey [Schedule_Project]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Project] DROP CONSTRAINT [Schedule_Project]
GO
/****** Object:  ForeignKey [Employee_Schedule]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Schedule] DROP CONSTRAINT [Employee_Schedule]
GO
/****** Object:  ForeignKey [Recurring_Schedule]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Schedule] DROP CONSTRAINT [Recurring_Schedule]
GO
/****** Object:  ForeignKey [Project_Task]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Task] DROP CONSTRAINT [Project_Task]
GO
/****** Object:  ForeignKey [Schedule_Task]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Task] DROP CONSTRAINT [Schedule_Task]
GO
/****** Object:  ForeignKey [Schedule_Task1]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Task] DROP CONSTRAINT [Schedule_Task1]
GO
/****** Object:  Table [dbo].[Task]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Task] DROP CONSTRAINT [Project_Task]
GO
ALTER TABLE [dbo].[Task] DROP CONSTRAINT [Schedule_Task]
GO
ALTER TABLE [dbo].[Task] DROP CONSTRAINT [Schedule_Task1]
GO
DROP TABLE [dbo].[Task]
GO
/****** Object:  Table [dbo].[Cases]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Cases] DROP CONSTRAINT [Customer_Case]
GO
ALTER TABLE [dbo].[Cases] DROP CONSTRAINT [Schedule_Case]
GO
ALTER TABLE [dbo].[Cases] DROP CONSTRAINT [Status_Case]
GO
DROP TABLE [dbo].[Cases]
GO
/****** Object:  Table [dbo].[Project]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Project] DROP CONSTRAINT [Schedule_Project]
GO
DROP TABLE [dbo].[Project]
GO
/****** Object:  Table [dbo].[EmployeGroupFilters]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[EmployeGroupFilters] DROP CONSTRAINT [Employee_EmployeGroupFilter]
GO
ALTER TABLE [dbo].[EmployeGroupFilters] DROP CONSTRAINT [Group_EmployeGroupFilter]
GO
DROP TABLE [dbo].[EmployeGroupFilters]
GO
/****** Object:  Table [dbo].[LockedDates]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[LockedDates] DROP CONSTRAINT [Employee_LockedDate]
GO
DROP TABLE [dbo].[LockedDates]
GO
/****** Object:  Table [dbo].[Schedule]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Schedule] DROP CONSTRAINT [Employee_Schedule]
GO
ALTER TABLE [dbo].[Schedule] DROP CONSTRAINT [Recurring_Schedule]
GO
DROP TABLE [dbo].[Schedule]
GO
/****** Object:  Table [dbo].[Employee]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Employee] DROP CONSTRAINT [Group_Employee]
GO
DROP TABLE [dbo].[Employee]
GO
/****** Object:  Table [dbo].[Colors]    Script Date: 04/24/2013 21:35:08 ******/
DROP TABLE [dbo].[Colors]
GO
/****** Object:  Table [dbo].[Customers]    Script Date: 04/24/2013 21:35:08 ******/
DROP TABLE [dbo].[Customers]
GO
/****** Object:  Table [dbo].[Group]    Script Date: 04/24/2013 21:35:08 ******/
DROP TABLE [dbo].[Group]
GO
/****** Object:  Table [dbo].[Recurring]    Script Date: 04/24/2013 21:35:08 ******/
DROP TABLE [dbo].[Recurring]
GO
/****** Object:  Table [dbo].[Settings]    Script Date: 04/24/2013 21:35:08 ******/
DROP TABLE [dbo].[Settings]
GO
/****** Object:  Table [dbo].[Statuses]    Script Date: 04/24/2013 21:35:08 ******/
DROP TABLE [dbo].[Statuses]
GO
/****** Object:  Table [dbo].[Statuses]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Statuses](
    [StatusID] [int] IDENTITY(1,1) NOT NULL,
    [StatusDescription] [varchar](50) NULL,
 CONSTRAINT [PK_dbo.Statuses] PRIMARY KEY CLUSTERED 
(
    [StatusID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Settings]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Settings](
    [SettingID] [int] IDENTITY(1,1) NOT NULL,
    [SettingKey] [varchar](50) NOT NULL,
    [SettingValue] [text] NOT NULL,
 CONSTRAINT [PK_dbo.Settings] PRIMARY KEY CLUSTERED 
(
    [SettingID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Recurring]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Recurring](
    [RecurringID] [int] IDENTITY(1,1) NOT NULL,
    [Period] [int] NULL,
    [DateStart] [datetime] NULL,
    [DateLast] [datetime] NULL,
    [Advance] [int] NULL,
 CONSTRAINT [PK_dbo.Recurring] PRIMARY KEY CLUSTERED 
(
    [RecurringID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Group]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Group](
    [GroupID] [int] IDENTITY(1,1) NOT NULL,
    [GroupDescription] [varchar](max) NOT NULL,
    [IsReadOnly] [bit] NULL,
 CONSTRAINT [PK_dbo.[Group]]] PRIMARY KEY CLUSTERED 
(
    [GroupID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Customers]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Customers](
    [CustomerID] [int] IDENTITY(1,1) NOT NULL,
    [CustomerDescription] [varchar](max) NULL,
 CONSTRAINT [PK_dbo.Customers] PRIMARY KEY CLUSTERED 
(
    [CustomerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Colors]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Colors](
    [ColorID] [int] IDENTITY(1,1) NOT NULL,
    [ColorValue] [int] NOT NULL,
 CONSTRAINT [PK_dbo.Colors] PRIMARY KEY CLUSTERED 
(
    [ColorID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Employee]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Employee](
    [EmployeID] [int] IDENTITY(1,1) NOT NULL,
    [EmployeName] [varchar](50) NOT NULL,
    [EmployeEmail] [varchar](50) NOT NULL,
    [EmployePassword] [char](44) NOT NULL,
    [DefaultNumWeek] [smallint] NULL,
    [GroupID] [int] NULL,
 CONSTRAINT [PK_dbo.Employee] PRIMARY KEY CLUSTERED 
(
    [EmployeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Schedule]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Schedule](
    [ScheduleID] [int] IDENTITY(1,1) NOT NULL,
    [DateFrom] [datetime] NULL,
    [Hours] [decimal](10, 2) NULL,
    [EmployeID] [int] NULL,
    [RecurringID] [int] NULL,
    [Priority] [int] NULL,
    [DateTo] [datetime] NULL,
    [IsLocked] [bit] NOT NULL,
    [BumpPriority] [int] NULL,
 CONSTRAINT [PK_dbo.Schedule] PRIMARY KEY CLUSTERED 
(
    [ScheduleID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[LockedDates]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[LockedDates](
    [LockedDateID] [int] IDENTITY(1,1) NOT NULL,
    [Date] [datetime] NOT NULL,
    [IsYearly] [bit] NOT NULL,
    [EmployeeID] [int] NULL,
 CONSTRAINT [PK_dbo.LockedDates] PRIMARY KEY CLUSTERED 
(
    [LockedDateID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[EmployeGroupFilters]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EmployeGroupFilters](
    [EmployeGroupFilterID] [int] IDENTITY(1,1) NOT NULL,
    [EmployeID] [int] NOT NULL,
    [GroupID] [int] NOT NULL,
 CONSTRAINT [PK_dbo.EmployeGroupFilters] PRIMARY KEY CLUSTERED 
(
    [EmployeGroupFilterID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Project]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Project](
    [ProjectID] [int] IDENTITY(1,1) NOT NULL,
    [ProjectTitle] [varchar](max) NOT NULL,
    [ProjectDescription] [varchar](max) NOT NULL,
    [ScheduleID] [int] NULL,
    [EstimatedHours] [decimal](10, 2) NULL,
    [IsRetired] [bit] NULL,
    [Color] [int] NULL,
    [PaymoID] [int] NULL,
 CONSTRAINT [PK_dbo.Project] PRIMARY KEY CLUSTERED 
(
    [ProjectID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Cases]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Cases](
    [CaseID] [int] IDENTITY(1,1) NOT NULL,
    [CaseTitle] [varchar](max) NOT NULL,
    [CaseDescription] [varchar](max) NOT NULL,
    [CustomerID] [int] NOT NULL,
    [ScheduleID] [int] NULL,
    [EstimatedHours] [decimal](10, 2) NULL,
    [StatusID] [int] NULL,
    [Color] [int] NULL,
    [CRMID] [varchar](50) NULL,
 CONSTRAINT [PK_dbo.Cases] PRIMARY KEY CLUSTERED 
(
    [CaseID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Task]    Script Date: 04/24/2013 21:35:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Task](
    [TaskID] [int] IDENTITY(1,1) NOT NULL,
    [TaskTitle] [varchar](max) NOT NULL,
    [TaskDescription] [varchar](max) NOT NULL,
    [ProjectID] [int] NULL,
    [ScheduleID] [int] NULL,
    [PaymoID] [int] NULL,
    [ParentScheduleID] [int] NULL,
    [IsUnscheduelable] [bit] NULL,
    [EstimatedHours] [decimal](10, 2) NULL,
    [PaymoUser] [varchar](max) NULL,
    [PaymoOrder] [int] NULL,
    [PaymoEstimateHours] [decimal](10, 2) NULL,
 CONSTRAINT [PK_dbo.Task] PRIMARY KEY CLUSTERED 
(
    [TaskID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  ForeignKey [Customer_Case]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Cases]  WITH CHECK ADD  CONSTRAINT [Customer_Case] FOREIGN KEY([CustomerID])
REFERENCES [dbo].[Customers] ([CustomerID])
GO
ALTER TABLE [dbo].[Cases] CHECK CONSTRAINT [Customer_Case]
GO
/****** Object:  ForeignKey [Schedule_Case]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Cases]  WITH CHECK ADD  CONSTRAINT [Schedule_Case] FOREIGN KEY([ScheduleID])
REFERENCES [dbo].[Schedule] ([ScheduleID])
GO
ALTER TABLE [dbo].[Cases] CHECK CONSTRAINT [Schedule_Case]
GO
/****** Object:  ForeignKey [Status_Case]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Cases]  WITH CHECK ADD  CONSTRAINT [Status_Case] FOREIGN KEY([StatusID])
REFERENCES [dbo].[Statuses] ([StatusID])
GO
ALTER TABLE [dbo].[Cases] CHECK CONSTRAINT [Status_Case]
GO
/****** Object:  ForeignKey [Group_Employee]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Employee]  WITH CHECK ADD  CONSTRAINT [Group_Employee] FOREIGN KEY([GroupID])
REFERENCES [dbo].[Group] ([GroupID])
GO
ALTER TABLE [dbo].[Employee] CHECK CONSTRAINT [Group_Employee]
GO
/****** Object:  ForeignKey [Employee_EmployeGroupFilter]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[EmployeGroupFilters]  WITH CHECK ADD  CONSTRAINT [Employee_EmployeGroupFilter] FOREIGN KEY([EmployeID])
REFERENCES [dbo].[Employee] ([EmployeID])
GO
ALTER TABLE [dbo].[EmployeGroupFilters] CHECK CONSTRAINT [Employee_EmployeGroupFilter]
GO
/****** Object:  ForeignKey [Group_EmployeGroupFilter]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[EmployeGroupFilters]  WITH CHECK ADD  CONSTRAINT [Group_EmployeGroupFilter] FOREIGN KEY([GroupID])
REFERENCES [dbo].[Group] ([GroupID])
GO
ALTER TABLE [dbo].[EmployeGroupFilters] CHECK CONSTRAINT [Group_EmployeGroupFilter]
GO
/****** Object:  ForeignKey [Employee_LockedDate]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[LockedDates]  WITH CHECK ADD  CONSTRAINT [Employee_LockedDate] FOREIGN KEY([EmployeeID])
REFERENCES [dbo].[Employee] ([EmployeID])
GO
ALTER TABLE [dbo].[LockedDates] CHECK CONSTRAINT [Employee_LockedDate]
GO
/****** Object:  ForeignKey [Schedule_Project]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Project]  WITH CHECK ADD  CONSTRAINT [Schedule_Project] FOREIGN KEY([ScheduleID])
REFERENCES [dbo].[Schedule] ([ScheduleID])
GO
ALTER TABLE [dbo].[Project] CHECK CONSTRAINT [Schedule_Project]
GO
/****** Object:  ForeignKey [Employee_Schedule]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Schedule]  WITH CHECK ADD  CONSTRAINT [Employee_Schedule] FOREIGN KEY([EmployeID])
REFERENCES [dbo].[Employee] ([EmployeID])
GO
ALTER TABLE [dbo].[Schedule] CHECK CONSTRAINT [Employee_Schedule]
GO
/****** Object:  ForeignKey [Recurring_Schedule]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Schedule]  WITH CHECK ADD  CONSTRAINT [Recurring_Schedule] FOREIGN KEY([RecurringID])
REFERENCES [dbo].[Recurring] ([RecurringID])
GO
ALTER TABLE [dbo].[Schedule] CHECK CONSTRAINT [Recurring_Schedule]
GO
/****** Object:  ForeignKey [Project_Task]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Task]  WITH CHECK ADD  CONSTRAINT [Project_Task] FOREIGN KEY([ProjectID])
REFERENCES [dbo].[Project] ([ProjectID])
GO
ALTER TABLE [dbo].[Task] CHECK CONSTRAINT [Project_Task]
GO
/****** Object:  ForeignKey [Schedule_Task]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Task]  WITH CHECK ADD  CONSTRAINT [Schedule_Task] FOREIGN KEY([ScheduleID])
REFERENCES [dbo].[Schedule] ([ScheduleID])
GO
ALTER TABLE [dbo].[Task] CHECK CONSTRAINT [Schedule_Task]
GO
/****** Object:  ForeignKey [Schedule_Task1]    Script Date: 04/24/2013 21:35:08 ******/
ALTER TABLE [dbo].[Task]  WITH CHECK ADD  CONSTRAINT [Schedule_Task1] FOREIGN KEY([ParentScheduleID])
REFERENCES [dbo].[Schedule] ([ScheduleID])
GO
ALTER TABLE [dbo].[Task] CHECK CONSTRAINT [Schedule_Task1]
GO

我在那里放了一些东西,因为数据库可能已经设置好了,在这种情况下我想要一个新的副本。

我得到:

An exception occurred while executing a Transact-SQL statement or batch.

我在做什么有什么问题吗?我从使用 SQL Server 生成脚本获得了脚本。

我只是想让它:

 1. Clear any tables, just empty the db.

2. Create the tables and foreign keys.

谢谢

4

5 回答 5

1

它失败是因为当您将完整脚本作为简单字符串传递给 ExecuteNonQuery 时,无法识别 GO 字。但是,接受 StringCollection 对象的 ExecuteNonQuery的重载表示接受由 GO 分隔的脚本

我会这样尝试:(未测试)

StringCollection col = new StringCollection();
col.Add(script);
server.ConnectionContext.ExecuteNonQuery(col);

如果这不起作用,那么想到的唯一解决方法是

string goSplitter = new string[] { "\r\nGO\r\n" };
string[] cmdParts = script.Split(goSplitter, StringSplitOptions.RemoveEmptyEntries);
StringCollection col = new StringCollection();
col.AddRange(cmdParts);
server.ConnectionContext.ExecuteNonQuery(col);

更新 我已经使用您提供的代码对我的数据库测试了您脚本的一小部分,这与我对使用单个字符串的 ExecuteNonQuery文档的理解相反,它按预期工作。因此,整个脚本中可能存在一些语法错误或其他问题。我建议在 Sql Management Studio 控制台中对其进行测试

更新 2
现在我真的很好奇,所以我启动了 Reflector 来调查 ExecuteNonQuery 的代码,现在这个谜团已经解决了。这些是获取单个字符串的方法的第一个反编译行。

public int ExecuteNonQuery(string sqlCommand, ExecutionTypes executionType)
{
    int num6;
    this.CheckDisconnected();
    int statementsToReverse = 0;
    int num2 = 0;
    StringCollection strings = this.GetStatements(sqlCommand, executionType, ref statementsToReverse);
    ........

因此,单个字符串被传递给一个名为 GetStatements 的内部方法,该方法返回一个 StringCollection

于 2013-04-24T22:18:22.950 回答
0

Phil Haack 有一篇文章描述了他创建的一个库,该库允许您运行包含GO语句的 SQL 注释。

http://haacked.com/archive/2007/11/04/a-library-for-executing-sql-scripts-with-go-separators-and.aspx

您需要使用这样的库来运行您拥有的脚本。

于 2013-04-24T22:22:13.667 回答
0

然而,其他人已经声明 GO 是 SSMS 的一部分;是 sql 的一部分,所以如果你用 ; 结束每条语句的最后一行 而不是 go,它应该从您的代码内部执行。

于 2013-04-24T22:24:52.293 回答
0

您的 SQL 中有错误。我复制并粘贴了它,运行了两次(以创建表结构),我收到错误消息,例如

无法删除对象“dbo.Employee”,因为它被 FOREIGN KEY 约束引用。

您必须以正确的顺序删除表格。从具有引用的表开始...例如先删除 [EmployeGroupFilters] 然后 Employee 等。

祝你好运。

于 2013-04-25T00:04:49.180 回答
0

该脚本不是纯 SQL。GO 语句不是 SQL 语言的一部分,而是编辑器预处理器的一部分,用于分隔 SQL 批处理语句。您不能使用 SqlCommand 和 ExecuteNonQuery 方法执行此类脚本。

你需要的是 sqlcmd.exe http://msdn.microsoft.com/en-us/library/ms162773.aspx

于 2013-04-24T22:19:22.380 回答