• 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();
            }
    
    
    
    
    
        }
    }
  • 相关阅读:
    (转)运行pip报错:Fatal error in launcher: Unable to create process using '"'
    (转)pycharm autopep8配置
    Hash
    Java 8 Lambda表达式
    位运算
    PHP数组操作大全
    php final static const成员属性用法
    Java编程性能优化
    java中double变量保留小数问题
    encodeURIComponent编码后java后台的解码
  • 原文地址:https://www.cnblogs.com/homchou/p/2838017.html
Copyright © 2020-2023  润新知