简体   繁体   English

从设置保存和加载强类型数据集

[英]Saving and Loading Strongly-Typed DataSet from Settings

I added a strongly-typed DataSet object to my project. 我向项目添加了强类型的DataSet对象。 It's type name is DocRetrieverDataSet . 它的类型名称是DocRetrieverDataSet I also have in my project settings a row for a user-scope DataSet property named DocRetrieverDataSource to which I want to save an instance of DocRetrieverDataSet . 我的项目设置中还存在一个用户范围的DataSet属性行,该属性名为DocRetrieverDataSource,我想将DocRetrieverDataSet的实例保存到该DocRetrieverDataSet

Here is boiled-down code: 这是简化的代码:

using Settings = MyProjectNameSpace.Properties.Settings;
....
private DocRetrieverDataSet myDocRetrieverDataSet;

public myForm()
{
    Initialize();
    if (Settings.Default.DocRetrieverDataSource == null)
    {
        Settings.Default.DocRetrieverDataSource = new DocRetrieverDataSet();
        Settings.Default.Save();
    }
    this.myDocRetrieverDataSet = (DocRetrieverDataSet)Settings.Default.DocRetrieverDataSource;
}

The first time I run it, when Settings.Default.DocRetrieverDataSource is null , it works fine! 我第一次运行它时,当Settings.Default.DocRetrieverDataSourcenull ,它运行良好! However, when I run it the second time, I get an InvalidCastException at 但是,当我第二次运行它时,我在以下位置收到InvalidCastException

this.myDocRetrieverDataSet = (DocRetrieverDataSet)Settings.Default.DocRetrieverDataSource;

It says 它说

Unable to cast object of type 'System.Data.DataSet' to type 'DocRetriever.DocRetrieverDataSet'.

The funny thing is that it doesn't have this problem the first time around. 有趣的是,它第一次没有这个问题。 What is going on and how can I fix it? 怎么回事,我该如何解决?

MORE INFO: Here's the relevant code from Settings.Designer.cs 更多信息:这是来自Settings.Designer.cs的相关代码

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Data.DataSet DocRetrieverDataSource {
    get {
        return ((global::System.Data.DataSet)(this["DocRetrieverDataSource"]));
    }
    set {
        this["DocRetrieverDataSource"] = value;
    }
}

And from app.config 并从app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=abcdefghijkl" >
            <section name="DocRetriever.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=abcdefghijkl" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <DocRetriever.Properties.Settings>
            <setting name="SpoolDirectoryPath" serializeAs="String">
                <value />
            </setting>
            <setting name="OutputDirectoryPath" serializeAs="String">
                <value />
            </setting>
        </DocRetriever.Properties.Settings>
    </userSettings>
</configuration>

You need to change the casting in your Settings file from DataSet to your DocRetrieverDataSet 您需要将“设置”文件中的强制类型从“数据集”更改为DocRetrieverDataSet

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Data.DataSet DocRetrieverDataSource {
    get {
        return ((global::FullNamespace.DocRetrieverDataSet)(this["DocRetrieverDataSource"]));
    }
    set {
        this["DocRetrieverDataSource"] = value;
    }
}

You can also do it via the Settings Designer, just go to your property and browse for you class 您也可以通过“设置设计器”进行操作,只需转到您的媒体资源并浏览您的课程即可 布置设计师

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM