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(); } } }