且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#将所有datagridview行添加到mysql?

更新时间:2023-12-06 17:15:16

,con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1。 DataSource = dt;
}
private void dataGridView1_MouseClick( object sender,MouseEventArgs e)
{
DataGridViewCell cell = null ;
foreach (DataGridViewCell selectedCell in dataGridView1.SelectedCells)
{
cell = selectedCell;
break ;
}
if (cell!= null
{
DataGridViewRow row = cell.OwningRow;
txt_stt.Text = row.Cells [ STT]。Value.ToString() ;
txt_ngay_nhap.Text = row.Cells [ NgàyNhập]。Value.ToString( );
txt_ten_tb.Text = row.Cells [ Tênthiếtbị]。Value.ToString ();
txt_nha_sx.Text = row.Cells [ Nhàsảnxuất]。Value.ToString ();
txt_donvi.Text = row.Cells [ Đơnvị]。Value.ToString( );
txt_soluong.Text = row.Cells [ Sốlượng]。Value.ToString( );
txt_nhan_tu.Text = row.Cells [ Nhậntừ]。Value.ToString( );
txt_chip_ID.Text = row.Cells [ Chip ID]。Value.ToString( );
txt_mac.Text = row.Cells [ MAC地址]。Value.ToString( );
txt_sn.Text = row.Cells [ SN]。Value.ToString() ;
txt_note.Text = row.Cells [ Ghichú]。Value.ToString( );
}
}
私有 void btn_add_Click( object sender,EventArgs e)
{
// 插入mysql
.......
}
",con); DataTable dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; } private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { DataGridViewCell cell = null; foreach (DataGridViewCell selectedCell in dataGridView1.SelectedCells) { cell = selectedCell; break; } if (cell != null) { DataGridViewRow row = cell.OwningRow; txt_stt.Text = row.Cells["STT"].Value.ToString(); txt_ngay_nhap.Text = row.Cells["Ngày Nhập"].Value.ToString(); txt_ten_tb.Text = row.Cells["Tên thiết bị"].Value.ToString(); txt_nha_sx.Text = row.Cells["Nhà sản xuất"].Value.ToString(); txt_donvi.Text = row.Cells["Đơn vị"].Value.ToString(); txt_soluong.Text = row.Cells["Số lượng"].Value.ToString(); txt_nhan_tu.Text = row.Cells["Nhận từ"].Value.ToString(); txt_chip_ID.Text = row.Cells["Chip ID"].Value.ToString(); txt_mac.Text = row.Cells["MAC address"].Value.ToString(); txt_sn.Text = row.Cells["SN"].Value.ToString(); txt_note.Text = row.Cells["Ghi chú"].Value.ToString(); } } private void btn_add_Click(object sender, EventArgs e) { // insert to mysql ....... }


谢谢,我已经解决了我的问题

Thank you, I have solved my problem
private void btn_all_Click(object sender, EventArgs e)
        {
            try
            {
                connect_data();
                byte[] imageBt = null;

                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    string STT = dataGridView1.Rows[i].Cells["STT"].Value.ToString();
                    string Ngay_nhap = dataGridView1.Rows[i].Cells["Ngày nhập"].Value.ToString();
                    string Ten_thiet_bi = dataGridView1.Rows[i].Cells["Tên thiết bị"].Value.ToString();
                    string Nha_san_xuat = dataGridView1.Rows[i].Cells["Nhà sản xuất"].Value.ToString();
                    string Don_vi = dataGridView1.Rows[i].Cells["Đơn vị"].Value.ToString();
                    string So_luong = dataGridView1.Rows[i].Cells["Số lượng"].Value.ToString();
                    string Nhan_tu = dataGridView1.Rows[i].Cells["Nhận từ"].Value.ToString();
                    string Chip_ID = dataGridView1.Rows[i].Cells["Chip ID"].Value.ToString();
                    string MAC_address = dataGridView1.Rows[i].Cells["MAC address"].Value.ToString();
                    string SN = dataGridView1.Rows[i].Cells["SN"].Value.ToString();
                    string Ghi_chu = dataGridView1.Rows[i].Cells["Ghi chú"].Value.ToString();
                    string Ngay_xuat = "";
                    string Ngay_nhan_lai = "";
                    string Query = "INSERT into nhap_2015 (STT,Ngay_nhap,Ten_thiet_bi,Nha_san_xuat,Don_vi,So_luong,Nhan_tu,Chip_ID,MAC_address,SN,Ghi_chu,Hinh,Ngay_xuat,Ngay_nhan_lai) values ('" + STT + "','" + Ngay_nhap + "','" + Ten_thiet_bi + "','" + Nha_san_xuat + "','" + Don_vi + "','" + So_luong + "','" + Nhan_tu + "','" + Chip_ID + "','" + MAC_address + "','" + SN + "','" + Ghi_chu + "',@hinh,'" + Ngay_xuat + "','" + Ngay_nhan_lai + "');";
                    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDatabase);
                    MySqlDataReader myReader;
                    try
                    {
                        conDatabase.Open();
                        cmdDataBase.Parameters.Add(new MySqlParameter("@hinh", imageBt));
                        myReader = cmdDataBase.ExecuteReader();
                        //MessageBox.Show("SAVE");
                        while (myReader.Read())
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    conDatabase.Close();
                }
                MessageBox.Show("ADD COMPLETED");
            }
            catch
            {
                MessageBox.Show("Error!!!!!");
            }
        }