简体   繁体   English

无法在Windows 8中使用SQL Server 2008 R2创建数据库

[英]Can't create database in Windows 8 with SQL Server 2008 R2

I'm trying to run install scripts that create a database for our application they have been working until our upgrade to Windows 8. Installed SQL Server 2008 R2 Express and run scripts that execute the following to create the DB: 我正在尝试运行为我们的应用程序创建数据库的安装脚本,这些脚本在升级到Windows 8之前一直在工作。安装SQL Server 2008 R2 Express并运行执行以下命令的脚本以创建数据库:

USE [master]
GO

SET NUMERIC_ROUNDABORT OFF
GO
SET XACT_ABORT, ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS, NOCOUNT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

IF EXISTS (SELECT name FROM sys.databases WHERE name = N'$(db)')
BEGIN
    ALTER Database [$(db)] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    DROP Database [$(db)]
END
GO

CREATE DATABASE [$(db)]
GO
USE  [$(db)]
GO

I can run the following command line to execute the script: 我可以运行以下命令行来执行脚本:

sqlcmd -i test.sql -S .\wizrms -d test -U sa -P W1zard

I get the following error: 我收到以下错误:

Msg 4060, Level 11, State 1, Server .\\WIZRMS, Line 1 消息4060,级别11,状态1,服务器。\\ WIZRMS,第1行
Cannot open database "test" requested by the login, The login failed. 无法打开登录请求的数据库“测试”,登录失败。

Msg 18456, Level 14, State 1 Server .\\WIZRMS, Line 1 消息18456,级别14,状态1服务器。\\ WIZRMS,第1行
Login failed for user 'sa'. 用户“ sa”的登录失败。

I can login using SQL Server Management Studio with the same credentials and create the database with the same command. 我可以使用具有相同凭据的SQL Server Management Studio登录,并使用相同的命令创建数据库。

And this all works on Windows 7. My assumption is there is a security change in Win 8 that I have to deal with. 所有这些都可以在Windows 7上运行。我的假设是Win 8中必须处理安全性更改。

It's probably because of the parameter -d test , specifying an initial database that doesn't yet exists. 可能由于参数-d test ,指定了尚不存在的初始数据库。 When the script you're running creates the database, you typically login in master DB first, then create the DB, then switch to it for all the remaining work. 当您运行的脚本创建数据库时,通常首先登录master数据库,然后创建数据库,然后切换到该数据库以完成所有剩余工作。 But trying to open that database before it has been created will for sure yield the error you're posting here. 但是,在创建数据库之前尝试打开该数据库肯定会产生您在此处发布的错误。

SSMS does exactly that. SSMS正是这样做的。 It always connect to master initially, then switches databases as you work on different DBs. 它始终始终先连接到master,然后在您使用不同的DB时切换数据库。 But the CREATE DATABASE command is always issued in the context of master . 但是CREATE DATABASE命令始终在master上下文中发出。

Windows version doesn't matters here. Windows版本在这里无关紧要。 Possibly, in your Win7 install you're connecting to a server with a pre-existing test database, while in your Win8 computer, it doesn't exists yet. 可能是,在Win7安装中,您正在连接到具有预先存在的test数据库的服务器,而在Win8计算机中,该服务器尚不存在。

Thanks Alejandro. 谢谢亚历杭德罗。

I changed -d to master and assign the database by using -v db = fleeb 我将-d更改为master并使用-v db = fleeb分配了数据库

my command line now reads: 我的命令行现在显示为:

sqlcmd -i test.sql -S sof202\\wizrms -d master -U sa -P W1zard -v db = fleeb sqlcmd -i test.sql -S sof202 \\ wizrms -d master -U sa -P W1zard -v db = fleeb

And it ran without error and created a database named fleeb. 它运行无误,并创建了一个名为fleeb的数据库。

Perfect. 完善。

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

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