如何在c#中保存数据网格视图和文本框中的数据

时间:2016-02-25 04:42:55

标签: c# mysql

我有3个文本框,数据网格视图有多列,我需要使用MySql将数据保存到数据库 我可以使用button1中的Update quire从文本框中将数据保存到数据库,并通过button2保存或从数据网格视图更新,但我需要更新并保存多个表中的(数据网格视图)和(文本框)中的数据在MySql中输入代码

 try
        {
            string conn = "Server=" + GlobalsForServerInfo.Server + "; Port=" + GlobalsForServerInfo.Port + "; Database=" + GlobalsForServerInfo.Database + "; Uid=" + GlobalsForServerInfo.Username + "; Pwd=" + GlobalsForServerInfo.Password + ";";
            string Query = "INSERT INTO accounting_sys.add_new_voucher (Voucher_Id,Voucher_Number,Date,Time,Status,Suppliers,Type) values('" + this.txtVoucherId.Text + "' ,'" + this.txtVouNo.Text + "'   ,    '" + this.dtpDateFrom.Text + "'   ,   '" + this.timeEdit1.Text + "'    ,    '" + this.txtStatus.Text + "','" + this.txtSuppliers.Text + "','" + Type + "') ;";
            MySqlConnection MyConn2 = new MySqlConnection(conn);
            MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
            MySqlDataReader MyReader2;
            MyConn2.Open();
            MyReader2 = MyCommand2.ExecuteReader();
            MessageBox.Show("Data Saved");
            while (MyReader2.Read())
                MyConn2.Close();

        }

        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }

    }

/

 private vobutton4_Clickid (object sender, EventArgs e)
        if (txtboxCredit.Text == textboxDebit.Text)
        {
            try //Try section 1 of operation
            {
                dataGridView1.EndEdit(); //very important step
                da.Update(dataTable);
                MessageBox.Show("Data Updated");
                DataBind();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }
        else
            MessageBox.Show(" Make sure that debit and credit account balances are in the appropriate debit and credit columns of the trial balance calculation. Check for reversed digits and misplaced decimal points.", "Error in transferring account balances to proper trial balance columns",
MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

0 个答案:

没有答案