gpt4 book ai didi

c# - 访问 Microsoft.SqlServer.Management.Smo.Server 对象的成员正好崩溃一次

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:24 24 4
gpt4 key购买 nike

我们正在尝试访问 Microsoft.SqlServer.Management.Smo.Server 对象的 DefaultFile 成员。调试了很多,发现第一次访问成员闪退,后来可以正常访问了。在调试窗口中查看变量第一次返回异常,第二次和所有后续时间返回正确值。

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

第一次访问崩溃是非常一致的,所以做一些像下面这样糟糕的事情可以完全解决这个问题:

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。但是,在运行代码时调试打印程序集版本时,我们找不到使用的版本有任何差异。

最佳答案

我在使用旧版本的 SMO 时看到过类似的消息,尤其是在尝试使用 2008R2 SMO 访问 2012 时。从 2008R2 Featurepack SP3 升级到安装解决了我的问题。

检查是否没有您可以使用的更新版本的共享管理对象 + CLR 类型 + native 客户端驱动程序。

关于c# - 访问 Microsoft.SqlServer.Management.Smo.Server 对象的成员正好崩溃一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32355226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com