2

我正在安装 SQL Server Express 2005 作为我的 c# windows 窗体应用程序的先决条件,并且在我的安装程序类的自定义操作下,我正在尝试执行下面的脚本,以在用户计算机上创建我的数据库。

但是,当我选择在 C:\ 驱动器(Windows 根文件夹)上创建数据库时,我收到此错误/异常:

Operating system error 5(access is denied)

如果我尝试在任何其他驱动器中这样做,我不会收到任何错误。

请帮我。如何获得此 SQL 查询的相关权限?

USE [master]
GO
/****** Object:  Database [Society_security_database]    Script Date: 08/11/2012 14:26:23 ******/
CREATE DATABASE [Society_security_database] ON  PRIMARY 
( NAME = N'Society_security_database', FILENAME = N'C:\Society_security_database.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'Society_security_database_log', FILENAME = N'C:\Society_security_database.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
 COLLATE SQL_Latin1_General_CP1_CI_AS
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'Society_security_database', @new_cmptlevel=90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Society_security_database].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [Society_security_database] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [Society_security_database] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [Society_security_database] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [Society_security_database] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [Society_security_database] SET ARITHABORT OFF 
GO
ALTER DATABASE [Society_security_database] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [Society_security_database] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [Society_security_database] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [Society_security_database] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [Society_security_database] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [Society_security_database] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [Society_security_database] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [Society_security_database] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [Society_security_database] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [Society_security_database] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [Society_security_database] SET  ENABLE_BROKER 
GO
ALTER DATABASE [Society_security_database] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [Society_security_database] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [Society_security_database] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [Society_security_database] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [Society_security_database] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [Society_security_database] SET  READ_WRITE 
GO
ALTER DATABASE [Society_security_database] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [Society_security_database] SET  MULTI_USER 
GO
ALTER DATABASE [Society_security_database] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [Society_security_database] SET DB_CHAINING OFF 
4

2 回答 2

4

终于找到了解决方案因为我试图从网络服务帐户访问数据库。当我将其更改为本地系统时,它起作用了!这是代码

Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer mc = new Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer();
                Microsoft.SqlServer.Management.Smo.Wmi.Service svc = mc.Services["MSSQL$SQLEXPRESS"];
                svc.SetServiceAccount("LocalSystem",null);
                svc.Alter();
                SqlConnection.ClearAllPools();

它将服务帐户更改为 localSystem

于 2012-09-06T09:14:41.053 回答
0

我遇到了类似的问题。经过大量分析后,我想出了一个简单的解决方法。关闭你的 Visual Studio。以管理员身份再次打开它,这应该可以工作。

希望能帮助到你。

于 2013-06-18T19:06:48.580 回答