标题:如何把datagridview中的数据快速保存到Access数据库中?
只看楼主
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
结帖率:50%
 问题点数:0 回复次数:4 
如何把datagridview中的数据快速保存到Access数据库中?
各位大侠,能传授一种可以快一点的办法把datagridview中的1000多行多列数据保存到Access数据库中?!

这里不是每列数据都需要保存,只是其中的6或7列(根据需要--可能每次都不一样)需要保存,使用insert into的方法可以实现,但是速度太慢了?
搜索更多相关主题的帖子: 保存 Access datagridview 数据库 
2010-09-10 13:43
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
得分:0 
大侠们啊,帮忙想想办法啊?!
2010-09-11 19:51
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
得分:0 
找了一天的相关材料,终于搞定了,现在和大家分享一下:
首先:把不需要的列删除了
 try
            {
                dataGridView_import.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
                dataGridView_import.CurrentCell.Selected = true;
                if (dataGridView_import.Columns.Count > 0)
                {

                    if (MessageBox.Show("Are you sure to delete the Entire Column?!", "Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
                    {
                        
                        string columnname = dataGridView_import.Columns[dataGridView_import.CurrentCell.ColumnIndex].Name.ToString();
                        dataGridView_import.Columns.Remove(columnname);
                    }
                    
                }
                else
                {
                    deleteEntireColumnToolStripMenuItem.Enabled = false;
                }
                dataGridView_import.SelectionMode = DataGridViewSelectionMode.CellSelect;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
然后:借用dataadaptor,把datagridview中的数据装到dataset的datatable里面:
                    OleDbCommand  mycom = new OleDbCommand(sqlstr, conn);
                    OleDbDataAdapter oledataadap = new OleDbDataAdapter(mycom);
                    DataSet  ds = new DataSet();
                    oledataadap.Fill(ds,"Table");
                    DataTable dt = ds.Tables["Table"];
                    dataGridView_Survey_data.DataSource = dt;
                    int totalcolumn = dataGridView_import.RowCount;
                        for (int i = 0; i < totalcolumn; i++)
                        {
                            DataRow temprow = dt.NewRow();
                            temprow["D"] = depthmdv;
                            temprow["td"] = tvdv;
                            temprow["RO"] = ropv;
                            temprow["WO"] = wohv;
                            temprow["WB"] = wobv;
                            temprow["RM"] = rpmv;
                            temprow["fr"] = flowv;
                            temprow["SP"] = sppv;
                            temprow["TQ"] = tqv;
                            temprow["Muty"] = mudv;
                            temprow["Comments"] = commentsv;
                            dt.Rows.Add(temprow);
                         }
                        OleDbCommandBuilder oledbcb = new OleDbCommandBuilder(oledataadap);
                        DataSet datasetchange = ds.GetChanges();
                        if (datasetchange != null)
                        {

                            oledataadap.Update(datasetchange);
                            label1.Text = "Saving to DataBase...";
                            ds.AcceptChanges();
                            label1.Text = "Saved to DataBase!!!";
                        }
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        label1.Text = ex.Message;
                    }
                    finally
                    {
                        mycom.Dispose();
                        conn.Dispose();
                    }
这里面的 depthmdv和其他的变量在前面赋值: =Convert.ToSingle( dataGridView_import.Rows[i].Cells[inde].Value);
后面用到OleDbCommandBuilder的一些方法,不是很熟,也不敢多说,只是自己试着使用一下,觉得还行,基本可以实现自己的设想。。。。。
2010-09-12 14:19
novker
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2008-10-20
得分:0 
不错,正好用到,谢谢。
2010-10-10 10:51
luhuijuan
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-6-23
得分:0 
正好要用到 谢谢!
2011-06-26 22:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-319950-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057627 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved