将多个数据和数据类型从子窗体传递给父窗体并保存到数据库

时间:2016-06-29 05:50:35

标签: c#

在我的MdiForm中,我有一个有btnSave的菜单栏。我想通过单击midForm菜单栏上的btnSave来保存在我的子窗体中创建的数据。我有这些类将数据保存到数据库:

StudentBal.cs

internal class StudentBal
{
    public string DateAdded { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string CityAddress { get; set; }
    public string HomeAddress { get; set; }
    public string Nationality { get; set; }
    public string MaritalStatus { get; set; }
    public string DateOfBirth { get; set; }
    public string PlaceOfBirth { get; set; }
    public string TelNo { get; set; }
    public string Father { get; set; }
    public string Mother { get; set; }
    public string ParentsAddress { get; set; }
    public string EmployersName { get; set; }
    public string EmplyerTelNo { get; set; }
    public string GuardiansName { get; set; }
    public string GuradiansAddress { get; set; }
    public byte[] StudentImage { get; set; }
    public string WorkAddress { get; set; }

    public int InsertStudent(StudentBal bal)
    {
        var dal = new StudentDal();
       return dal.InsertStudent(bal);
    }

StudentDal.cs

public int InsertStudent(StudentBal bal)
    {
        const string query =
            @"INSERT INTO students VALUES(@DateAdded, @FirstName, @MiddleName, @LastName, @CityAddress, @HomeAddress, @Nationality,
                                @MaritalStatus, @DateOfBirth, @PlaceOfBirth, @TelNo, @Father, @Mother, 
                                @ParentsAddress, @EmployersName, @EmployersTelNo, @GuardiansName, 
                                @GuardiansAddress, @StudentImage, @WorkAddress)";
        using (_cmd = new SqlCommand(query, _cn))
        {
            _cmd.Parameters.AddWithValue("@DateAdded", Convert.ToDateTime(bal.DateAdded).ToShortDateString());
            _cmd.Parameters.AddWithValue("@FirstName", bal.FirstName);
            _cmd.Parameters.AddWithValue("@MiddleName", bal.MiddleName);
            _cmd.Parameters.AddWithValue("@LastName", bal.LastName);
            _cmd.Parameters.AddWithValue("@CityAddress", bal.CityAddress);
            _cmd.Parameters.AddWithValue("@HomeAddress", bal.HomeAddress);
            _cmd.Parameters.AddWithValue("@Nationality", bal.Nationality);
            _cmd.Parameters.AddWithValue("@MaritalStatus", bal.MaritalStatus);
            _cmd.Parameters.AddWithValue("@DateOfBirth", bal.DateOfBirth);
            _cmd.Parameters.AddWithValue("@PlaceOfBirth", bal.PlaceOfBirth);
            _cmd.Parameters.AddWithValue("@TelNo", bal.TelNo);
            _cmd.Parameters.AddWithValue("@Father", bal.Father);
            _cmd.Parameters.AddWithValue("@Mother", bal.Mother);
            _cmd.Parameters.AddWithValue("@ParentsAddress", bal.ParentsAddress);
            _cmd.Parameters.AddWithValue("@EmployersName", bal.EmployersName);
            _cmd.Parameters.AddWithValue("@EmployersTelNo", bal.EmplyerTelNo);
            _cmd.Parameters.AddWithValue("@GuardiansName", bal.GuardiansName);
            _cmd.Parameters.AddWithValue("@GuardiansAddress", bal.GuradiansAddress);
            _cmd.Parameters.AddWithValue("@StudentImage", bal.StudentImage);
            _cmd.Parameters.AddWithValue("@WorkAddress", bal.WorkAddress);
            _cn.Open();
            return(_cmd.ExecuteNonQuery());
        }
    }

这是我的AddStudent表格

 private void btnSaveInformation_Click(object sender, EventArgs e)
    {
        var stream = new MemoryStream();
        pictureBox2.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] pic = stream.ToArray();
        if (!CheckTextBox()) return;
        var bal = new StudentBal
        {
            CityAddress = string.IsNullOrWhiteSpace(txtCityAddress.Text) ? "N/A" : txtCityAddress.Text,
            DateAdded = DateTime.Now.ToShortDateString(),
            DateOfBirth = txtDateOfBirth.Text,
            EmployersName = string.IsNullOrWhiteSpace(txtEmployersName.Text) ? "N/A" : txtEmployersName.Text,
            EmplyerTelNo = string.IsNullOrWhiteSpace(txtEmpContactNumber.Text) ? "N/A" : txtEmpContactNumber.Text,
            Father = string.IsNullOrWhiteSpace(txtFathersName.Text) ? "N/A" : txtFathersName.Text,
            FirstName = txtFirstName.Text,
            GuardiansName = string.IsNullOrWhiteSpace(txtGuardiansName.Text) ? "N/A" : txtGuardiansName.Text,
            GuradiansAddress = string.IsNullOrWhiteSpace(txtGuardiansAddress.Text) ? "N/A" : txtGuardiansAddress.Text,
            HomeAddress = txtHomeAddress.Text,
            LastName = txtLastName.Text,
            MaritalStatus = txtMaritalStatus.Text,
            MiddleName = string.IsNullOrWhiteSpace(txtMiddleName.Text) ? "N/A" : txtMiddleName.Text,
            Mother = txtMothersName.Text,
            Nationality = txtNationality.Text,
            ParentsAddress = txtParentsAddress.Text,
            PlaceOfBirth = txtPlaceOfBirth.Text,
            TelNo = string.IsNullOrWhiteSpace(txtTelNo.Text) ? "N/A" : txtTelNo.Text,
            StudentImage = pic,
            WorkAddress = string.IsNullOrWhiteSpace(txtWorkAddress.Text) ? "N/A" : txtWorkAddress.Text, 
        };
        var result = bal.InsertStudent(bal);
        if (result > 0)
        {
            MessageBox.Show(@"Data successfully added.");
        }
        var obj = (MdiForm)Application.OpenForms["MdiForm"];
        if (obj != null) obj.FillComboBox(); 
    }

我需要将我的childform(AddStudent)中的所有信息传递给父窗体(MdiFOrm),这样我就可以通过单击菜单栏中的btnSave来保存所有细节。我尝试创建一个可以在父表单中调用的公共方法(SaveDetails()),如下所示:

AddStudent add = new AddStudent();
add.SaveDetails();

但是,执行上面的代码将创建一个新实例,并且不会保存具有要保存的值的活动子窗体。如果有一种方法可以调用活动子窗体中的方法而不实例化(var addStudent = new AddStudent();)那将是很好的。 将方法设为public static在此方案中不起作用。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这是进行回调的抽象示例:

public class Parent
{
    public void CreateChild()
    {
        Child childNew = new Child(this); //here you give over the parents reverence
    }

    public void SaveStuff(int number)
    {
        //here you can save the number
    }
}


class Child
{
    private Parent parent;
    public Child(Parent parent)
    {
        this.parent = parent;
    }

    public void PressOkButton()
    {
        this.parent.SaveStuff(4); //here you are doing the callback
    }
}