AccessImport demo

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace AccessImport_v1._0
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        OleDbConnection conn = new OleDbConnection();
        OleDbCommand com = new OleDbCommand();

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Excel文件|*.xls";
            if (of.ShowDialog() == DialogResult.OK)
            {
                textBox_ExcelFile.Text = of.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog of1 = new OpenFileDialog();
            of1.Filter = "Access文件|*.mdb";
            if (of1.ShowDialog() == DialogResult.OK)
            {
                textBox_AccessFile.Text = of1.FileName;
            }
        }

        public void import()
        {
            try
            {
                conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox_AccessFile.Text;
                com.Connection = conn;
                conn.Open();
                if (radioButton1.Checked == false)
                {
                    com.CommandText = "delete from " + textBox_AccessTableName.Text.Trim();
                    com.ExecuteNonQuery();
                }
                com.CommandText = "insert into " + textBox_AccessTableName.Text.Trim() + " (" + textBox_AccessField.Text.Trim()
                    + ") select " + textBox_ExcelField.Text.Trim() + " from [Excel 8.0;database="
                    + textBox_ExcelFile.Text.Trim() + "].[sheet1$] ";
                com.ExecuteNonQuery();
                MessageBox.Show("Import Success!");

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            import();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            about ab = new about();
            ab.ShowDialog();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            Application.Exit();
        }





    }
}

 

转载于:https://www.cnblogs.com/homchou/archive/2012/12/28/2838017.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值