我正在尝试使用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.
由于
答案 0 :(得分:1)
它失败,因为当您将完整脚本作为简单字符串传递给ExecuteNonQuery时,无法识别GO字。但是,声明采用StringCollection对象的overload of 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 with a single string的文档的理解相反,它已按预期工作。因此,整个脚本中可能存在语法错误或其他问题。我建议在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
答案 1 :(得分:0)
该脚本不是纯SQL。 GO语句不是SQL语言的一部分,而是编辑器预处理器的一部分,用于分隔SQL批处理语句。您无法使用SqlCommand和ExecuteNonQuery方法执行此类脚本。
您需要的是sqlcmd.exe http://msdn.microsoft.com/en-us/library/ms162773.aspx
答案 2 :(得分:0)
Phil Haack有一篇文章描述了他创建的一个库,它允许您运行包含GO
语句的SQL注释。
您需要使用此类库来运行您拥有的脚本。
答案 3 :(得分:0)
其他人已经说过GO是SSMS的一部分;是sql的一部分,所以如果你结束每个语句的最后一行;而不是go,它应该从你的代码中执行。
答案 4 :(得分:0)
您的SQL中有错误。 我运行复制并粘贴它,运行它两次(创建表结构),我收到错误消息,如
无法删除对象'dbo.Employee',因为它是由FOREIGN KEY约束引用的。
您必须以正确的顺序删除表。 从包含引用的表开始... 示例drop firt [EmployeGroupFilters]然后是Employee等。
祝你好运。