用于创建数据库的 Linq to Sql 执行命令
我有一个 ASP.NET MVC 应用程序,并且使用 LinqToSql 进行数据库访问。
我想从 SQL 脚本创建一个数据库到我的 DBMS (SQL Server Express) 中。
当我运行我的脚本时,没有 SQL Server Management Studio,它可以使用相同的用户连接,但是,当我使用 ExecuteCommand
从 LinqToSql 运行它时,它确实会给出有关我无法使用 GO 语句的错误。如果我删除 GO 语句并重试,它会显示 CREATE SCHEMA 应该是批处理的第一行。
因此,我很困惑,是否有任何其他(更好)的方式从 asp.net mvc 运行包含 CREATE DATABASE 和 CREATE SCHEMA 语句的 sql 脚本而没有这些问题。
谢谢, cas sakal
这是脚本的内容;
USE [master]
GO
CREATE DATABASE [TESTDB]
CREATE SCHEMA [base] AUTHORIZATION [dbo]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [base].[TestTable](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[IsActive] [bit] NOT NULL,
CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED
(
[Id] 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
I have a ASP.NET MVC application and I am using LinqToSql for database access.
I want to create a database from a SQL script into my DBMS (SQL Server Express).
When I run my script no SQL Server Management Studio it works with same user connection, however, when I run it from LinqToSql with ExecuteCommand
it does give errors about I cannot use GO statements. If I remove GO statements and try again, it says CREATE SCHEMA should be the first line of the batch.
Therefore, I am confused, is there any other(better) way of running the sql script, which contains CREATE DATABASE and CREATE SCHEMA statements, from asp.net mvc without these issues.
Thanks,
cas sakal
Here is the content of the script;
USE [master]
GO
CREATE DATABASE [TESTDB]
CREATE SCHEMA [base] AUTHORIZATION [dbo]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [base].[TestTable](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[IsActive] [bit] NOT NULL,
CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED
(
[Id] 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用:
This works for me:
这些任务必须从脚本执行吗?您可以使用 SQL Server 管理对象,它为您提供 以编程方式访问管理任务。例如创建数据库
Do these task have to be executed from a script? You could use SQL Server Management Objects which gives you programmatic access to management tasks. e.g creating a database
GO
不是有效的 T-SQL 命令 - 它是 SQL Server Management Studio 使用的命令分隔符。您不能在要从自己的代码执行的 SQL 脚本中使用GO
。您需要做的就是将该脚本分成两个:
CREATE DATABASE
脚本用于创建数据库(以及可能的架构),您需要在其他使用 ExecuteCommand 调用。
GO
is not a valid T-SQL command - it's a command delimiter that SQL Server Management Studio uses. You cannot useGO
in SQL scripts you want to execute from your own code.What you need to do is split up that script into two:
CREATE DATABASE
script that creates the database (and possibly schema)You need to execute both one after the other using the
ExecuteCommand
call.