c#如何将datagridview中的多行保存到一个数据库

gk7wooem  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(721)

我有2个datagridview,需要将它们保存在一个数据库中。
我不知道怎么解释这个,但我在这里链接的视频,我直接从我的桌面。
youtube视频链接
这是我的保存按钮代码。

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

}

}

093gszye

093gszye1#

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

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

在form load事件中,datagridview用动态datatable填充

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

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

循环在datagridview行上执行。在循环中,从datagridview的每一列(单元格)获取值,并将记录插入到数据库表中。

相关问题