将数据表添加到数据集会在数据集中创建 2 个相同的表,还有其他添加方法吗?

Adding datatable to dataset creates 2 identical tables in dataset, is there another way to add?

仅供参考,我知道有不同的方法来处理这个问题,但我需要它以我的项目的这种特定方式工作。

我从我在 VS2010 中使用 DataSetGenerator 创建的 DataSet 创建了一个 DataSet(DS) 和一个 DataTable(DT),然后我用一些数据向 DT 添加了一行。然后我尝试将 DT(其中包含一行数据)添加到 DS。此时 Visual Studio 当我进入下一行代码时,DataSet Visualizer 显示我有一个包含 2 个同名表的数据集,但是,其中一个有数据,另一个没有。这可能是我的疏忽,但我无法识别。仍然在这里学习 C#,所以这也无济于事。感谢您的帮助!

旧代码

private string getStorageKey(string strStorageAccount)
    {
        DataSetStorageKeyDetails ds = new DataSetStorageKeyDetails();
        DataSetStorageKeyDetails.StorageKeyDetailsDataTable dt = new DataSetStorageKeyDetails.StorageKeyDetailsDataTable();
        string strStorageKey = "";

        dt.Rows.Add(strStorageAccount);
        ds.Tables.Add(dt);

        DataSet dsOut = ServiceEnclosed.InterfaceService("GetStorageKey", ds);

        DataTable dtr = dsOut.Tables[0];
        DataSetStorageKeyDetails.StorageKeyDetailsRow dr = dt.First();
        strStorageKey = dr.StorageName;            

        return strStorageKey;
    }

新代码

private string getStorageKey(string strStorageAccount)
    {
        DataSetStorageKeyDetails ds = new DataSetStorageKeyDetails();
        string strStorageKey = "";

        ds.StorageKeyDetails.Rows.Add(strStorageAccount);
        DataSet dsOut = ServiceEnclosed.InterfaceService("GetStorageKey", ds);

        DataTable dtr = new DataTable();
        dtr = dsOut.Tables[0];
        strStorageKey = dtr.Rows[0]["StorageKey"].ToString();

        return strStorageKey;
    }

当您使用 DataSetGenerator 创建数据集时,您指定的 table 会在创建数据集时自动创建。您不必添加新的 table,只需访问已有的即可。

private string getStorageKey(string strStorageAccount)
    {
        DataSetStorageKeyDetails ds = new DataSetStorageKeyDetails();
        string strStorageKey = "";

        ds.StorageKeyDetails.Rows.Add(strStorageAccount);

        DataSet dsOut = ServiceEnclosed.InterfaceService("GetStorageKey", ds);

        DataTable dtr = dsOut.Tables[0];
        strStorageKey = ds.StorageKeyDetails.Rows[0].StorageName;            

        return strStorageKey;
    }

未经测试的代码,但我认为它为您指明了正确的方向。