共享DataSet多个表单C# [英] Shared DataSet Over Multiple Forms C#

查看:104
本文介绍了共享DataSet多个表单C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在一个数据库项目工作,在我的第一个形式我有sql连接命令访问我的数据源。 Ive还创建了一个数据集,这一切都在我的显示形式中。

So im working on a database project, in my first form i have sql connection command which accesses my datasource. Ive also created a dataset this is all in my displayform

显示数据库用于显示数据库,我添加了一个按钮来添加记录,所以当我点击添加记录,它会去添加到我可以填写细节来创建一个新的联系人。然后回到第一个表单来显示新创建的联系人和所有其他人。

The displayform is used for displaying the database, i've added a button to add records, so when i click on add records it would go to the addform where i can fill in the details to creating a new contact. Then go back to the first form to display the newly created contact and all others.

但是,由于数据集需要相同,我有一些问题作为显示形式的一个。

However i'm having a bit of a problem as the dataset needs to be the same as the one in the display form.

如何使数据集在所有表单中保持一致?

How can i make the dataset be the same across all my forms?

更新:

所以我在Program.cs中创建的对象在那里创建了对象,并使它们成为public static。

So what ive done is in my Program.cs ive created the objects there... and made them public static.

public static DataSet ds = new DataSet();

所以在我的addcontact表单中,我可以这样调用...

so then in my addcontact form i can call it like this...

Program.ds.Clear();

与我的dataadaptor / bindingsource和sql连接相同。这样做可以吗?

Same with my dataadaptor/bindingsource and sql connection. Is this ok to do?

推荐答案

通过构造函数创建数据集类添加通过每个表单..
Class = Class作为参考。不是副本。 (DataSet是一个类...)

Create a dataset class add pass in through the constructor to every form.. A "Class = Class" makes a reference .. not a copy. ( DataSet is a Class... )

public partial class Form1 : Form
{
DataSet _dataset;

public Form1(DataSet dataSet)
{
    _dataset = dataset;
    InitializeComponent();
}
//..

public partial class Form2 : Form
{
DataSet _dataset;

public Form2(DataSet dataSet)
{
    _dataset = dataset;
    InitializeComponent();
}
//..


static class Program
{
    static void Main()
    {
        DataSet DS = new DataSet();

        Application.Run(new Form1(DS));
    }
}

这篇关于共享DataSet多个表单C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆