首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SMO FileGroup.Files产生DuplicateNameException

SMO FileGroup.Files产生DuplicateNameException
EN

Stack Overflow用户
提问于 2015-04-15 15:14:25
回答 1查看 185关注 0票数 0

对于我的ASP.net应用程序,我正在实现一种使用(SMO)库代码自动克隆生产数据库的方法。为此,我将备份数据库,然后将其还原到同一服务器上的不同数据库。作为其中的一部分,我使用relocateFiles集合来指定新数据库的数据文件和日志文件的位置。这在Server 2008R2上没有问题;但是,当连接到我的SQL Server 2012实例时,尝试相同的操作时,我会遇到以下异常:

System.Data.DuplicateNameException“一个名为'DatabaseName2‘的列已经属于这个DataTable。”

此异常发生在以下代码的第二行:

代码语言:javascript
复制
foreach (FileGroup fg in mySourceDatabase.FileGroups){
    foreach (DataFile df in fg.Files){
        string location = df.FileName;
        int extensionLoc = location.IndexOf(".mdf");
        if (extensionLoc > 0) {
            string newLocation = location.Substring(0, extensionLoc - 1);
            newLocation = newLocation + "_clone.mdf";
            restoreDB.RelocateFiles.Add(new RelocateFile(df.Name, newLocation));
        }
        else {
            extensionLoc = location.IndexOf(".ndf");
            if (extensionLoc > 0) {
                string newLocation = location.Substring(0, extensionLoc - 1);
                newLocation = newLocation + "_clone.ndf";
                restoreDB.RelocateFiles.Add(new RelocateFile(df.Name, newLocation));
            }
        }
    }
}

堆栈跟踪如下:

在System.Data.DataColumnCollection.RegisterColumnName(String名称处,DataColumn列在System.Data.DataColumnCollection.BaseAdd(DataColumn列上)在System.Data.DataColumnCollection.AddAt(Int32索引,DataColumn列)在Microsoft.SqlServer.Management.Smo.DataProvider.Init(StatementBuilder sb,RetriveMode rm在Microsoft.SqlServer.Management.Smo.ExecuteSql.GetDataProvider(StringCollection查询,Object con,StatementBuilder sb,RetriveMode rm)在Microsoft.SqlServer.Management.Smo.SqlObjectBase.FillData(ResultType resultType,StringCollection sql,Object connectionInfo,在Microsoft.SqlServer.Management.Smo.SqlObjectBase.FillDataWithUseFailure(SqlEnumResult广场( StatementBuilder sb),在Microsoft.SqlServer.Management.Smo.SqlObjectBase.BuildResult(EnumResult ( ResultType resultType) ),在Microsoft.SqlServer.Management.Smo.SqlObjectBase.GetData(EnumResult erParent(Microsoft.SqlServer.Management.Smo.SqlObjectBase.GetData(EnumResult erParent),在Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData(Request req ( Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData() ),在Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.GetData(Object connectionInfo ( Object ci),(请求请求)(在Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorDataReader(Request req)在Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitChildLevel(Urn levelFilter,ScriptingPreferences sp,Boolean ( Microsoft.SqlServer.Management.Smo.SmoCollectionBase.InitializeChildCollection(Boolean刷新))在Microsoft.SqlServer.Management.Smo.SmoCollectionBase.GetEnumerator() (我上面代码的第二行)

我询问过sys.master_files,但看不到任何副本。

有人能告诉我问题出在哪里吗?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-11-10 11:43:46

好的,我从未设法找出导致FileGroup.Files枚举失败的原因;但是我发现,通过检查备份中文件的细节,我可以获得相同的信息:

代码语言:javascript
复制
Restore restoreDB = new Restore();
restoreDB.Database = settings.DestDBName;
restoreDB.Action = RestoreActionType.Database;
restoreDB.Devices.AddDevice(defaultBackupDir + backupFileRelativePath, DeviceType.File);
restoreDB.ReplaceDatabase = true;
restoreDB.NoRecovery = false;

DataTable fileList = restoreDB.ReadFileList(destinationServer);
foreach (DataRow row in fileList.Rows) {
    RelocateFile file = new RelocateFile();
    file.LogicalFileName = row[0].ToString();

    // Derive the physical location as the current physical location 
    string currentLocation = row[1].ToString();
    string extension = ".mdf";
    int extensionLoc = currentLocation.IndexOf(extension);
    if (extensionLoc <= 0) {
        extension = ".ldf";
        extensionLoc = currentLocation.IndexOf(extension);
    }
    if (extensionLoc <= 0) {
        extension = ".ndf";
        extensionLoc = currentLocation.IndexOf(extension);
    }
    if (extensionLoc <= 0) {
        continue;
    }
    string newLocation = currentLocation.Substring(0, extensionLoc);
    newLocation = newLocation + "_clone" + extension;
    file.PhysicalFileName = newLocation;

    restoreDB.RelocateFiles.Add(file);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29654054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档