Shared DataSet Over Multiple Forms C#

2019-09-07 06:25发布

问题:

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?

UPDATE:

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();

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

Program.ds.Clear();

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

回答1:

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));
    }
}


回答2:

Well, you have several conceptual options. You seem to be thinking of giving access to the data set to the child form, which is certainly something that you could do, but in my eyes it would make sense for the child form to provide the information for a single record to the parent form. The child form doesn't need to know anything about the dataset, or all of the other records.

In general you should try to limit information so that it is only avaliable where it is needed; you'll reduce the possibility of bugs that way. It would be bad practice to just make the dataset a global variable, which is what making it public and static is doing. By doing that not only are you providing the entire set of data to the child form (and also letting it be modfied) but your letting the data be read or modified from anywhere in the entire program. That's just asking for trouble.

The general idea will likely look something like this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 childForm = new Form2();
        childForm.ShowDialog();
        string name = childForm.Name;
        string value = childForm.SomeOtherValue;
        //you can stick these properties in your dataset here.
    }
}

public partial class Form2 : Form
{
    private TextBox textbox1;
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Close();
    }

    public string Name { get { return textbox1.Text; } }
    public string SomeOtherValue { get { return "12345"; } }
}


标签: c# dataset