如何使用C#将所有datagridview行添加到mysql? [英] How to add all datagridview row to mysql using C#?

查看:100
本文介绍了如何使用C#将所有datagridview行添加到mysql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我想导入datagridview中的所有或多选行,从excel到mysql。

我将此任务分为3步

1 / load excel到datagridview

2 /将datagridview加载到文本框

3 /将文本框值添加到mysql

完成。但是现在如何使用C#将所有datagridview行添加到mysql?

现在我想要

1 / load excel到datagridview

2 /然后按下按钮并将datagridview中的所有行插入mysql(无需将datagridview加载到文本框)

请帮助我。谢谢。

这是我的三步代码:

Hello
I want to import all or multi - select rows in datagridview from excel to mysql.
I divide this task in 3 step
1/ load excel to datagridview
2/ load datagridview to textbox
3/ add textbox value to mysql
It is done. But now How to add all datagridview row to mysql using C#?
Now I want
1/ load excel to datagridview
2/ And then press button and insert all rows in datagridview to mysql (no need load datagridview to textbox)
Please help me. Thanks.
Here is my code for three step:

   private void btn_open_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Excel 2003(*.xls)|*.xls|Excel 2013(*.xlsx)|*.xlsx|ALL Files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                String picPath = dlg.FileName.ToString();
                txt_path.Text = picPath;
                
            }
        }
 private void btn_show_Click(object sender, EventArgs e)
        {
            string constr = "Provider=MicroSoft.Jet.OLEDB.4.0;Data Source=" + txt_path.Text + "; Extended Properties =\"Excel 8.0; HDR = Yes;\";";
            OleDbConnection con = new OleDbConnection(constr);
            OleDbDataAdapter sda = new OleDbDataAdapter("Select* From["+txt_sheet.Text+"$]",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
.......
    }

推荐答案

,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!!!!!");
            }
        }


这篇关于如何使用C#将所有datagridview行添加到mysql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆