如何将数据网格视图/数据集中的数据导出到Access数据库? [C#]

时间:2016-04-19 20:16:48

标签: database ms-access datagridview export tableadapter

所以我的标题非常自我解释,所以到目前为止我已经阅读了微软文档,并在这里观看了一些关于我的问题的Youtube视频。

首先我的项目代码在这里:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in dsEquipment.Tables[0].Rows)
            {
                DataRow dr = sQTDBDataSet.tblEquipment.NewRow();
                dr[0] = r[0];
                dr[1] = r[1];
                dr[2] = r[2];
                dr[3] = r[3];
                dr[4] = r[4];
                dr[5] = r[5];
                dr[6] = r[6];
                dr[7] = r[7];
                dr[8] = r[8];
                dr[9] = r[9];
                dr[10] = r[10];
                dr[11] = r[11];
                dr[12] = r[12];
                dr[13] = r[13];
                dr[14] = r[14];
                dr[15] = r[15];
                dr[16] = r[16];

                sQTDBDataSet.tblEquipment.Rows.Add(dr);
            }
            //tblEquipmentTableAdapter.Update(sQTDBDataSet.tblEquipment);
            //tableAdapterManager.UpdateAll(sQTDBDataSet);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.tblEquipmentBindingSource.EndEdit();
                this.tblEquipmentTableAdapter.Update(this.sQTDBDataSet.tblEquipment);
                MessageBox.Show("Update successful");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Update failed");
            }
        }

        private void btnReadExcel_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFD = new OpenFileDialog();

            if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string strfilename = OFD.FileName;
                txtFileName.Text = strfilename;
            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void tblEquipmentBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.tblEquipmentBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.sQTDBDataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'sQTDBDataSet.tblEquipment' table. You can move, or remove it, as needed.
            this.tblEquipmentTableAdapter.Fill(this.sQTDBDataSet.tblEquipment);

        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            try
            {
                // Establish connection between the c# application and the excel file.
                OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 12.0");
                // Reading the data from the excel file.
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Equipments$]", con);
                // All data from the file will be loaded into the dataset.
                da.Fill(dsEquipment);
                // Show in a message box how many rows of data there is. 
                //MessageBox.Show(dsEquipment.Tables[0].Rows.Count.ToString());
                // Show the data in the data grid view.
                dgEquipment.DataSource = dsEquipment.Tables[0];
            }
            catch
            {
                MessageBox.Show("Please select the SQT2 Excel sheet.");
            }
        }
    }
}

所以我第一次尝试解决这个问题是:

            //tblEquipmentTableAdapter.Update(sQTDBDataSet.tblEquipment);
            //tableAdapterManager.UpdateAll(sQTDBDataSet);

我没有错误但由于某种原因我的Access数据库没有显示更新。

我的第二次尝试如下:

private void btnOpenFile_Click(object sender, EventArgs e)
{
    try
    {
        // Establish connection between the c# application and the excel file.
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 12.0");
        // Reading the data from the excel file.
        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Equipments$]", con);
        // All data from the file will be loaded into the dataset.
        da.Fill(dsEquipment);
        // Show in a message box how many rows of data there is. 
        //MessageBox.Show(dsEquipment.Tables[0].Rows.Count.ToString());
        // Show the data in the data grid view.
        dgEquipment.DataSource = dsEquipment.Tables[0];
    }
    catch
    {
        MessageBox.Show("Please select the SQT2 Excel sheet.");
    }
}

这是一个单独的按钮,我从Microsofts文档中获取了此代码,并更改了数据集和数据库表的变量,没有错误......但仍然是我的访问数据库没有更新!

现在完全卡住,任何帮助表示赞赏! :)

谢谢!

0 个答案:

没有答案