C#如何将多行从Datagridview保存到一个数据库

时间:2018-06-24 19:08:26

标签: c# mysql datagridview

我有2个datagridview,需要将它们保存在一个数据库中。

我不知道如何解释这一点,但是在这里,我链接了直接从桌面制作的视频。

Youtube video link

这是此表单的“保存按钮”代码。

{
    public Invoices()
    {
        InitializeComponent();
    }

    MySqlConnection Connection = new MySqlConnection("server=localhost; database=lmsdb; user=root; password=; pooling = false; convert zero datetime=True");
    //MySqlConnection Connection = new MySqlConnection("server=35.186.146.59;database=lmsdb;uid=lmsdb;pwd=dbserver; pooling = false; convert zero datetime=True");
    BindingSource bsInvoices = new BindingSource();


    private void Invoices_Load(object sender, EventArgs e)
    {
        BindGrid();
    }

    private void BindGrid()
    {
        MySqlConnection Connection = new MySqlConnection("server=localhost; database=lmsdb; user=root; password=; pooling = false; convert zero datetime=True");
        //Connection.Open();

        MySqlCommand Command = new MySqlCommand("Select * From invoicesdb", Connection);
        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(Command);

        da.Fill(dt);
        dgvInvoiceList.DataSource = null;
        dgvInvoiceList.AllowUserToAddRows = false;
        dgvInvoiceList.AutoGenerateColumns = false;
        dgvInvoiceList.ColumnCount = 16;
        dgvInvoiceList.Columns[0].HeaderText = "ID";
        dgvInvoiceList.Columns[0].DataPropertyName = "id";
        dgvInvoiceList.Columns[1].HeaderText = "Client Name";
        dgvInvoiceList.Columns[1].DataPropertyName = "ClientName";
        dgvInvoiceList.Columns[2].HeaderText = "References No";
        dgvInvoiceList.Columns[2].DataPropertyName = "ReferencesNo";
        dgvInvoiceList.Columns[3].HeaderText = "Case No";
        dgvInvoiceList.Columns[3].DataPropertyName = "CaseNo";
        dgvInvoiceList.Columns[4].HeaderText = "NRIC No";
        dgvInvoiceList.Columns[4].DataPropertyName = "NRICNo";
        dgvInvoiceList.Columns[5].HeaderText = "GST No";
        dgvInvoiceList.Columns[5].DataPropertyName = "GSTNo";
        dgvInvoiceList.Columns[6].HeaderText = "Invoices Date";
        dgvInvoiceList.Columns[6].DataPropertyName = "InvoicesDate";
        dgvInvoiceList.Columns[7].HeaderText = "Invoices Tax No";
        dgvInvoiceList.Columns[7].DataPropertyName = "InvoicesTaxNo";
        dgvInvoiceList.Columns[8].HeaderText = "Issue Date";
        dgvInvoiceList.Columns[8].DataPropertyName = "IssueDate";
        dgvInvoiceList.Columns[9].HeaderText = "Due Date";
        dgvInvoiceList.Columns[9].DataPropertyName = "DueDate";
        dgvInvoiceList.Columns[10].HeaderText = "Legal Fees";
        dgvInvoiceList.Columns[10].DataPropertyName = "LegalFees";
        dgvInvoiceList.Columns[11].HeaderText = "Stage";
        dgvInvoiceList.Columns[11].DataPropertyName = "StageSchedule";
        dgvInvoiceList.Columns[12].HeaderText = "Description";
        dgvInvoiceList.Columns[12].DataPropertyName = "Description";
        dgvInvoiceList.Columns[13].HeaderText = "Legal Fees Total";
        dgvInvoiceList.Columns[13].DataPropertyName = "LegalFeesTotal";
        dgvInvoiceList.Columns[14].HeaderText = "Disbursement Total";
        dgvInvoiceList.Columns[14].DataPropertyName = "DisbursementTotal";
        dgvInvoiceList.Columns[15].HeaderText = "Grand Total";
        dgvInvoiceList.Columns[15].DataPropertyName = "GrandTotal";
        dgvInvoiceList.Columns[0].Visible = false;
        dgvInvoiceList.DataSource = dt;
        Connection.Close();

        MySqlConnection Connection2 = new MySqlConnection("server=localhost; database=lmsdb; user=root; password=; pooling = false; convert zero datetime=True");
        //Connection.Open();

        MySqlCommand Command2 = new MySqlCommand("Select * From invoicesdb", Connection2);
        DataTable dt2 = new DataTable();
        MySqlDataAdapter da2 = new MySqlDataAdapter(Command2);

        da2.Fill(dt2);
        dgvDisList.DataSource = null;
        dgvDisList.AllowUserToAddRows = false;
        dgvDisList.AutoGenerateColumns = false;
        dgvDisList.ColumnCount = 2;
        dgvDisList.Columns[0].HeaderText = "Description";
        dgvDisList.Columns[0].DataPropertyName = "DisbursementDescription";
        dgvDisList.Columns[1].HeaderText = "Amount";
        dgvDisList.Columns[1].DataPropertyName = "DisbursementAmount";
        dgvDisList.DataSource = dt2;
        Connection2.Close();

    }

    private void btnSave_Click(object sender, EventArgs e)
    {

        MySqlConnection Connection = new MySqlConnection("server=localhost; database=lmsdb; user=root; password=; pooling = false; convert zero datetime=True");

        for (int i = 0; i < dgvDisList.Rows.Count; i++)
        {
            string disDescription = dgvDisList.Rows[i].Cells[0].Value.ToString();
            string disAmount = dgvDisList.Rows[i].Cells[1].Value.ToString();
            MySqlCommand cmd = new MySqlCommand($"INSERT INTO invoicesdb (id, ClientName, ReferencesNo, CaseNo, NRICNo, GSTNo, InvoicesDate, InvoicesTaxNo, IssueDate, DueDate, LegalFees, StageSchedule, Description, DisbursementDescription, DisbursementAmount, LegalFeesTotal, DisbursementTotal, GrandTotal) Values ('{tbID.Text}', {tbClientName.Text}','{tbReferencesNo.Text}','{tbCaseNo.Text}','{tbNRICNo.Text}','{tbGST.Text}','{dateInvoice.Text}','{tbInvoiceTaxNo.Text}','{dateIssue.Text}','{dateDue.Text}', '{tbLegalFees.Text}','{cbStageSchedule.Text}','{tbDescription.Text}','{ disDescription }','{ disAmount }','{tbTotalLegalFees.Text}','{tbTotalDisbursement.Text}','{tbGrandTotal.Text}')", Connection);

            Connection.Open();
            cmd.ExecuteNonQuery();
            Connection.Close();
        }

        Invoices_Load(sender, e);
        MessageBox.Show("Saved Successfully");
        btnNew_Click(sender, e);
    }

}

}

1 个答案:

答案 0 :(得分:0)

您将需要导入以下名称空间。

using System.Data;
using System.Data.SqlClient;

在“表单加载”事件中,使用动态DataTable填充了DataGridView

 private void Form1_Load(object sender, EventArgs e)
{
 this.BindDataGridView();
}

private void BindDataGridView()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new .....

在DataGridView行上执行循环。在循环内部,从DataGridView的每个列(单元)中获取值,并将记录插入数据库表中。