简体   繁体   English

C#WinForm列表到数据库

[英]C# WinForm List into Database

Afternoon All, 下午全部

I have been tasked with turning what is currently a paper-based form which our users fill in, into an electronic form in a Windows Form c# application, where the user can fill it in electronically then click a button, which puts the data in the database. 我的任务是将我们的用户当前填写的纸质表单转换为Windows Form c#应用程序中的电子表单,用户可以在其中以电子方式填写表单,然后单击一个按钮,将数据放入数据库。

I have already completed 5 other forms with no issues, however the one I have just reached, which I thought would be the simplest, has stumped me. 我已经完成了五种其他形式的表格,但都没有问题,但是我刚刚想到的一种形式(我认为这是最简单的一种形式)使我很困惑。

This is an example of what the paper one looks like and how it is filled in (it gets printed from excel first): 这是一个示例,说明纸的外观和填充方式(首先从excel打印):

纸质表格形式

My database has the following tables: 我的数据库具有下表:

User
   UserID
   UserName

EquipmentReturnSubmission
   UserID (from User table)
   ReturnID
   ReturnDate

 EquipmentReturnDetails
   ReturnID (from EquipmentReturnSubmission table)
   SerialNo
   Description

When the data is put into the database, each row on the form above will have a row in the EquipmentReturnDetails table, but all have the same ReturnID, so this can be linked to produce a list of the equipment submitted by that user. 将数据放入数据库后,上面表格中的每一行在EquipmentReturnDetails表中都会有一行,但是所有行都具有相同的ReturnID,因此可以链接以生成该用户提交的设备列表。

The bit that has stumped me is how to do this in my WinForms application. 让我感到困扰的是如何在WinForms应用程序中执行此操作。 I've had a look at inserting data from a GridView into a database, but can only find how to do this one row at a time - i need this to insert all of the rows using the same ReturnID so it can be linked. 我看过将GridView中的数据插入数据库中,但是一次只能找到如何执行这一行-我需要使用相同的ReturnID插入所有行,以便可以将其链接。

I thought I could do something like below, but not a clue where to start to get it coded, nor even if this is the best way to do it. 我以为我可以做下面的事情,但是找不到从哪里开始进行编码的线索,即使这是最好的方法。

尝试形式

My thinking is that the user enters the serial number and description, and clicks add, which puts the details into listbox/gridview or some kind of holding area, and clears the text boxes. 我的想法是,用户输入序列号和描述,然后单击添加,这会将详细信息放入列表框/ gridview或某种保存区域,并清除文本框。 The user can then keep doing this, each time the details are added to the holding area, then the submit button writes it to the database. 然后,每次将详细信息添加到保存区域时,用户就可以继续执行此操作,然后,提交按钮会将其写入数据库。

Again i'm not sure how this could be done unless there's a way to create a parameter each time the Add button is clicked. 同样,我不确定如何完成此操作,除非每次单击添加按钮时都有一种创建参数的方法。

If anyone could point me in the right direction that would be great. 如果有人能指出我正确的方向,那就太好了。 I'm self taught so happy to be completely corrected. 我很自学,很高兴能被完全纠正。

Thanks in advance. 提前致谢。

Well, thanks to Zath's comment I did a bit more research and managed to get this working with the below: 好吧,感谢Zath的评论,我做了更多的研究,并设法在以下方面使它起作用:

Tables: 表:

User
    UserID
    UserName

 EquipmentReturnSubmission
    UserID //(from User table)
    ReturnID
    ReturnDate

EquipmentReturnDetails
    ReturnID //(from EquipmentReturnSubmission table)
    SerialNo
    Description

I then added a DataGridView to my form, and added the columns SerialNo and Description. 然后,我将DataGridView添加到表单中,并添加了SerialNo和Description列。 My C# code below: 我的C#代码如下:

 private void Submit_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Are you sure these details are correct?", "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
                dataGridView1.AllowUserToAddRows = false; //Disables edit so the insert doesnt try and do an insert for the blank row created when the user clicks to add data.

                #region SQL Insert

                SqlConnection con = new SqlConnection(Home.ConString);
                SqlCommand cmd = new SqlCommand("EquipmentReturnSubmission1", con);
                cmd.CommandType = CommandType.StoredProcedure;
                #region Parameters

                cmd.Parameters.Add("@Name", SqlDbType.VarChar, 200).Value = OfficerName.Text;
                cmd.Parameters.Add("@Area", SqlDbType.VarChar, 200).Value = Area.Text;
                cmd.Parameters.Add("@SubmissionDate", SqlDbType.Date).Value = SubmissionDate.Value;
                cmd.Parameters.Add("@SubmissionID", SqlDbType.Int).Direction = ParameterDirection.Output;

                #endregion
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (System.Exception ex1)
                {
                    throw new System.Exception("Error submitting equipment return sheet." + ex1.Message);
                }
                finally
                {
                    SubID = int.Parse(Convert.ToString(cmd.Parameters["@SubmissionID"].Value));
                    con.Close();
                    SecondInsert();
                }
                #endregion
            }
        }
    }


    private void SecondInsert()
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {

            SqlConnection con = new SqlConnection(Home.ConString);
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO EquipmentReturnSubmissionDetails (SubmissionID, SerialNumber, Description) VALUES (@SubmissionID, @SerialNumber, @Description)", con);
                cmd.CommandType = CommandType.Text;
                {
                    cmd.Parameters.AddWithValue("@SubmissionID", SqlDbType.Int).Value = SubID;
                    cmd.Parameters.AddWithValue("@SerialNumber", row.Cells["SerialNo"].Value);
                    cmd.Parameters.AddWithValue("@Description", row.Cells["Description"].Value);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }

        }
        MessageBox.Show("Data submitted.");
        this.Close();
    }

This worked exactly as I required it to and does it very quickly. 这完全按照我的要求进行,并且很快完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM