WEB开发网
开发学院WEB开发ASP.NET C#将Excel数据表导入SQL数据库 阅读

C#将Excel数据表导入SQL数据库

 2012-06-05 14:15:25 来源:WEB开发网   
核心提示:方式二:先将Excel文件转换成DataTable,然后再循环将记录插入到数据库表中,C#将Excel数据表导入SQL数据库(2),这种方式可以任由程序员来选择将哪列数据导入到数据表的哪个字段中using System;using System.Collections.Generic;using System.Comp

方式二:
先将Excel文件转换成DataTable,然后再循环将记录插入到数据库表中,这种方式可以任由程序员来选择将哪列数据导入到数据表的哪个字段中

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

namespace ExcelToSQL
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        DataTable dt = new DataTable();
        string connString = "server = (local); uid = sa; pwd = sa; database = db_test";
        SqlConnection conn;

        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
                string fileName = fd.FileName;
                bind(fileName);
            }
        }
        private void bind(string fileName)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                 "Data Source=" + fileName + ";" +
                 "Extended Properties='Excel 8.0; HDR=Yes; IMEX=1'";
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT *  FROM [student$]", strConn);
            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
                dt = ds.Tables[0];
                this.dataGridView1.DataSource = dt;
            }
            catch (Exception err)
            {
                MessageBox.Show("操作失败!" + err.ToString());
            }
        }

        //将Datagridview1的记录插入到数据库  
        private void button2_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(connString);
            conn.Open();
            if (dataGridView1.Rows.Count > 0)
            {
                DataRow dr = null;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dr = dt.Rows[i];
                    insertToSql(dr);
                }
                conn.Close();
                MessageBox.Show("导入成功!");
            }
            else
            {
                MessageBox.Show("没有数据!");
            }
        }
        private void insertToSql(DataRow dr)
        {
            //excel表中的列名和数据库中的列名一定要对应
            string name = dr["StudentName"].ToString();
            string sex = dr["Sex"].ToString();
            string no = dr["StudentIDNO"].ToString();
            string major = dr["Major"].ToString();
            string sql = "insert into student values('" + name + "','" + sex + "','" + no + "','" + major +"')";         
            SqlCommand cmd = new SqlCommand(sql, conn);           
            cmd.ExecuteNonQuery();
        }
    }
}

上一页  1 2 

Tags:Excel 数据表 导入

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接