2012-09-27 93 views
-2

我已經在我的桌面上安裝了MS SQL Server 2012中,並試圖當我執行拋出連接數據庫失敗到SQL Server 2012

程序錯誤信息編寫C#程序

附加數據庫文件到SQL Server 2012,

如下更不用說了,可以請你幫我解決

低於MS SQL Server 2012中拋出的錯誤消息,

數據庫文件(MDF和LDF)

使用下面的代碼:

方法:

AttachDb("sqlDb","[C:\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\sqlDb.mdf] [C:Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\sqlDb_log.ldf]"); 

錯誤消息:

80131501 Error: An exception occurred while executing a Transact-SQL statement or batch. 
    From: Microsoft.SqlServer.ConnectionInfo 
    at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType) 
     at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType) 
     at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries) 
     at Microsoft.SqlServer.Management.Smo.Server.AttachDatabaseWorker(String name, StringCollection files, String owner, AttachOptions attachOptions) 
     at Microsoft.SqlServer.Management.Smo.Server.AttachDatabase(String name, StringCollection files) 
    80131500 Error: Attach database failed for Server 'sqlDbServer'. 
    From: Microsoft.SqlServer.Smo 
     at Microsoft.SqlServer.Management.Smo.Server.AttachDatabase(String name, StringCollection files) 
     at TestSMO.AttachDb(String dbname, String filelist) 

代碼:

 static string instance = ""; 
    static string username = ""; 
    static string password = ""; 
    static string connection_timeout = "600"; 
    static string statement_timeout = "3600"; 

    //create server connection object 
    static Server Connect() 
    { 
    ServerConnection conn = new ServerConnection(); 
    instance="sqlDbServer"; 
    if (instance.Length > 0) 
     conn.ServerInstance = instance; 

    if (username.Length > 0) { 
     conn.LoginSecure = false; 
     conn.Login = username; 
     conn.Password = password; 
    } 

    conn.ConnectTimeout = Convert.ToInt32(connection_timeout); 
     conn.StatementTimeout = Convert.ToInt32(statement_timeout); 
     return new Server(conn); 
    } 



    //performing attache DB 
    static void AttachDb(string dbname, string filelist) 
     { 
      string[] files = filelist.Split(new char[] {'['}, StringSplitOptions.RemoveEmptyEntries); 
      StringCollection dbfiles = new StringCollection(); 
      foreach (string s in files) { 
       string s1 = s.TrimEnd(null); 
       s1 = s1.TrimEnd(']'); 
       if (s1.Length > 0) dbfiles.Add(s1); 
      } 
      Server svr = Connect(); 
      string name = DecodeString(dbname); 
      svr.AttachDatabase(name, dbfiles); 
     } 
+1

這是否包含所有內部異常? SQL Server通常會生成更好的消息。 – usr

+0

爲什麼你把方括號放在文件名的周圍?他們是字符串,而不是標識符。 –

+0

嗨,感謝您的快速回復,因爲例外情況是「完全例外」取自CONSOL –

回答

-2

通過提供所有服務器角色NT AUTHORITY \發出解決SYSTEM登錄SQL S erver數據庫實例。