2015-09-02 46 views
0

我們正在嘗試訪問Microsoft.SqlServer.Management.Smo.Server對象的DefaultFile成員。經過大量調試後,結果第一次訪問成員崩潰,但可以很好地訪問。在調試窗口中查看變量是第一次返回一個異常,第二次和以後的所有時間都返回正確的值。訪問Microsoft.SqlServer.Management.Smo.Server對象的成員恰好崩潰一次

server.DefaultFileserver.DefaultLog第一接入拋出這樣一個異常:

System.IndexOutOfRangeException: Index was outside the bounds of the array. 
at Microsoft.SqlServer.Management.Smo.BitStorage.SetBit(Int32 itemIndex, BitIndex bitIndex, Boolean value) 
at Microsoft.SqlServer.Management.Smo.PropertyCollection.SetRetrieved(Int32 index, Boolean val) 
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.AddObjectPropsFromDataReader(IDataReader reader, Boolean skipIfDirty, Int32 startColIdx, Int32 endColIdx) 
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ImplInitialize(String[] fields, OrderBy[] orderby) 
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.Initialize(Boolean allProperties) 
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.OnPropertyMissing(String propname, Boolean useDefaultValue) 
at Microsoft.SqlServer.Management.Smo.PropertyCollection.GetValueWithNullReplacement(String propertyName, Boolean throwOnNullValue, Boolean useDefaultOnMissingValue) 
at Microsoft.SqlServer.Management.Smo.Server.get_DefaultFile() 
at my function... etc etc 

的第一次訪問碰撞非常concistent,所以做什麼可怕的事情像下面周圍完全問題得到:

using (var sqlConnection = new SqlConnection(sourceDatabaseConnectionString.ConnectionString)) 
{ 
    var svrConnection = new ServerConnection(sqlConnection); 
    var server = new Server(svrConnection); 

    // begin mega hack 
    try 
    { 
     // crashes on first access 
     var dummy = server.DefaultFile; 
    } 
    catch 
    { 
     // do absolutely nothing 
    } 
    // end mega hack 

    // business as usual... 
    var defaultFile = server.DefaultFile; 
} 

當然,我們不喜歡這樣的代碼(即使它只存在於我們的內部工具中),所以我們歡迎任何有關這種情況的原因。我們唯一的線索是我們最近將SQL Server 2012的一些實例升級到了SQL Server 2014.在安裝了SQL Server 2014的計算機上本地運行代碼時,我們有時可以解決這個問題。運行此代碼的生產計算機根本沒有安裝,但引用了DLL。但是,在運行代碼時調試打印程序集版本時,我們找不到使用版本的任何差異。

+0

首先,SMO在日期結束時點擊數據庫,您是否有權查看「VIEW SERVER STATE/DEFINITION」? – lad2025

+0

第二:用戶數據庫文件的默認位置是否與系統數據庫文件相同?如果是這樣的話,DefaultFile和DefaultLog屬性永遠不會被初始化。參見[這裏](http://sqlblog.com/blogs/allen_white/archive/2009/02/19/finding-your-default-file-locations-in-smo.aspx),PowerShell等價物('if($ server .DefaultFile){$ server.DefaultFile} else {$ server.InstallDataDirectory +'\'+'Data'}') – lad2025

回答

0

我在使用舊版SMO時看到過類似的消息,尤其是在試圖使用2008R2 SMO訪問2012年時。升級到2008R2 Featurepack SP3的安裝修復了我的問題。

檢查是否沒有您可以使用的共享管理對象+ CLR類型+本機客戶端驅動程序的更新版本。