[英]Exception thrown trying to create a DB with SMO C#
我正在嘗試使用SMO創建數據庫。 我在Create()
上收到此異常:
Microsoft.SqlServer.Management.Smo.FailedOperationException:創建數據庫'DataBase.t'失敗。 ---> Microsoft.SqlServer.Management.Common.ExecutionFailureException:執行Transact-SQL語句或批處理時發生異常。 ---> System.Data.SqlClient.SqlException:發生文件激活錯誤。 物理文件名“ Resources \\ DataBase.t.mdf;” 可能不正確。 診斷並糾正其他錯誤,然后重試該操作。 CREATE DATABASE失敗。 列出的某些文件名無法創建。 檢查相關錯誤。 在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand,ExecutionTypes執行類型)處的Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction操作,對象execObject,DataSet fillDataSet,布爾catchException)-內部的結尾異常堆棧跟蹤---在Microsoft.SqlServer.Management處Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand,ExecutionTypes執行類型)處Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand,ExecutionTypes執行類型) Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImplFinish(StringCollection createQuery,ScriptingPreferences sp)上的Microsoft.SqlServer.Management.Smo.SqlSmoObject.ExecuteNonQuery(StringCollection查詢,布爾值includeDbContext)上的.Smo.ExecutionManager.ExecuteNonQuery(StringCollection查詢) .SqlServer.Management.Smo.SqlSmoObject.CreateImpl()-結尾於 ner異常堆棧跟蹤---在d中的CustomSQLConnectionTest.Program.Main(String [] args)的Microsoft.SqlServer.Management.Smo.Database.Create()的Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImpl() \\ Visual Studio 2013 \\ Projects \\ DatabaseCreationTest \\ DatabaseCreationTest \\ Program.cs:第58行
我嘗試了可能的解決方案,但沒有任何效果。 我也以管理員和SQL Server Management Studio的身份運行VS,並為所有用戶設置了權限。 我也嘗試在帶有目錄的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。
我究竟做錯了什么?
我認為您正在嘗試創建全名'Resources\\DataBase.t.mdf;'
的文件'Resources\\DataBase.t.mdf;'
。 嘗試像執行SQL示例一樣提供一些正確的名稱,例如'C:\\Program Files\\Microsoft SQL Server\\MSSQL10.SQLEXPRESS\\MSSQL\\DATA\\test1.mdf'
,並且應該可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.