【问题标题】:Dataset retrieving again数据集再次检索
【发布时间】:2013-01-14 17:05:51
【问题描述】:

是否可以检索已经填充的数据Dataset?示例我从form1 填充数据集“Customer”,然后我想再次检索form2 中的数据集“Customer”而不执行 SQL 查询。

【问题讨论】:

    标签: c# dataset


    【解决方案1】:

    在第二个表单上创建一个DataSet 属性,并将值从form1 的数据集中传递到此属性。

    public class Form1
    {
        public DataSet ds;
    
        // You have already filled your DataSet so I'll leave that code out
        public void ShowForm2()
        {
            Form2 frm = new Form2();
            frm.MyDataSet = ds;
            frm.Show();
        }
    }
    
    public class Form2
    {
        public DataSet MyDataSet { get; set; }
    }
    

    【讨论】:

    • 怎么样? ... 只是 C# 的新手
    • 哦,我明白了...... >
    【解决方案2】:
    public class Form1
    {
        private DataSet _myDataSet;
        // do things
        private void fillMyDataSet()
        {
            //fill your dataset
        }
        public dataSet GetMyDataSet()
        {
            if(_myDataSet != null)
                return _myDataSet;
            else
            {
                return null;
            }
        }
    }
    

    那么在您的表格 2 中,您所要做的就是:

    DataSet myOtherDataSet = Form1.GetMyDataSet();
    

    【讨论】:

    • 哦...像 getter 和 setter...它是否也适用于修改后的数据集?...就像我第一次检索数据集时...iv'e 删除了一些行并对其进行编辑.....还有......数据集不是以编程方式制作的......就像我在Visual Studio上添加新项目......
    • 它应该适用于修改后的数据集,只需将 _myDataSet 分配给您添加的数据集的值。
    • 哦...所以在数据表中...(预制)iv'e 将其命名为 JODT...那么我该如何声明它?
    • 填写完数据表后,只需声明_myDataTable = JODT;
    【解决方案3】:

    您可以将其存储在Form1 的属性中,并将Form1 的实例传递给Form2

    public class Form1:Form 
    {
        public DataSet Data { get; set; }
    
        public void ShowForm2()
        {
            Form2 child = new Form2(this);
        }
    }
    
    public class Form2 : Form
    {
        public Form2(Form1 parent) { Parent = parent; }
        public Form1 Parent { get; set; }
    
        public void SomeMethod()
        {
            // now you can use the DataSet of Form1 via Parent proprty:
            DataSet data = this.Parent.Data;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-06
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      相关资源
      最近更新 更多