尝试使用SMO C#创建数据库时抛出异常

时间:2015-03-06 18:25:37

标签: c# sql sql-server sql-server-2012

我正在尝试使用SMO创建数据库。我在Create()

上得到了这个例外
  

Microsoft.SqlServer.Management.Smo.FailedOperationException:为Database' DataBase.t'创建失败。 ---> Microsoft.SqlServer.Management.Common.ExecutionFailureException:执行Transact-SQL语句或批处理时发生异常。 ---> System.Data.SqlClient.SqlException:发生文件激活错误。物理文件名' Resources \ DataBase.t.mdf;'可能不正确。诊断并更正其他错误,然后重试该操作。   CREATE DATABASE失败。无法创建列出的某些文件名。检查相关错误。   在Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction操作,Object execObject,DataSet fillDataSet,Boolean catchException)   在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand,ExecutionTypes executionType)   ---内部异常堆栈跟踪结束---   在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand,ExecutionTypes executionType)   在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands,ExecutionTypes executionType)   在Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection查询)   在Microsoft.SqlServer.Management.Smo.SqlSmoObject.ExecuteNonQuery(StringCollection queries,Boolean includeDbContext)   在Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImplFinish(StringCollection createQuery,ScriptingPreferences sp)   在Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImpl()   ---内部异常堆栈跟踪结束---   在Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImpl()   在Microsoft.SqlServer.Management.Smo.Database.Create()   在D:\ Visual Studio 2013 \ Projects \ DatabaseCreationTest \ DatabaseCreationTest \ Program.cs中的CustomSQLConnectionTest.Program.Main(String [] args):第58行

我尝试了解决方案而没有任何效果。我以管理员身份运行VS,也运行SQL Server Management Studio,我将权限设置为所有用户。此外,我尝试在SSMS中使用我的目录运行此查询,它实际上确实有效:

USE [master]
GO
/****** Object:  Database [bioenergiasIpad]    Script Date: 08/07/2012 17:01:19 ******/
CREATE DATABASE [test1] ON  PRIMARY 
( NAME = N'test1', 
  FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\test1.mdf', 
  SIZE = 70656KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB)
 LOG ON 
( NAME = N'test1_log', 
  FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\test1_log.ldf', 
  SIZE = 164672KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

来源

我的代码是:

Console.WriteLine("Enter username");
string UserName = Console.ReadLine();
Console.WriteLine("Enter password");
string Password = Console.ReadLine();

string connectionString = null;
SqlConnection cnn ;

Server server = new Server();
server.ConnectionContext.LoginSecure = true;

try
{
    connectionString = @"Data Source=" + server.Name + @"\Administrator" +
                            @"AttachDbFilename=|DataDirectory|Resources\DataBase." + UserName + ".mdf;" +
                             "Integrated Security=True;";

    Database database = new Database(server, "DataBase." + UserName);
    database.FileGroups.Add(new FileGroup(database, "PRIMARY"));

    DataFile dtPrimary = new DataFile(database.FileGroups["PRIMARY"], "PriValue", @"Resources\DataBase." + UserName + ".mdf;");
    dtPrimary.Size = 77.0 * 1024.0;
    dtPrimary.GrowthType = FileGrowthType.KB;
    dtPrimary.Growth = 1.0 * 1024.0;

    database.FileGroups["PRIMARY"].Files.Add(dtPrimary);

    LogFile logFile = new LogFile(database, "Log", @"Resources\DataBase." + UserName + ".ldf");
    logFile.Size = 7.0 * 1024.0;
    logFile.GrowthType = FileGrowthType.Percent;
    logFile.Growth = 10.0;

    database.LogFiles.Add(logFile);

    database.Create();
    database.Refresh();
}

我尝试了不同的连接字符串。

哦,我正在使用SQL Server 2012。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我认为您正在尝试创建一个名为'Resources\DataBase.t.mdf;'的文件。尝试提供一些正确的名称,就像你在SQL示例中所做的那样,例如'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\test1.mdf',它应该没问题。