c# winform excel导入sqlite数据库,求代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.OleDb;
using TenderClear.Common;
using TenderClear.BLL.TC01;

namespace TenderClear.UI.Forms.TC01
{
public partial class TC010103Form : DevExpress.XtraEditors.XtraForm
{
public TC010103Form()
{
InitializeComponent();
}
DataTable dt = new DataTable();
DataGridView dgv=new DataGridView();
private void txtExcel_Click(object sender, EventArgs e)
{
//打开excel选择框
OpenFileDialog frm = new OpenFileDialog();
frm.Filter = "Excel文件(*.xls,xlsx)|*.xls;*.xlsx";
if (frm.ShowDialog() == DialogResult.OK)
{

            txtExcel.Text = frm.FileName;
            bind(txtExcel.Text);
        }
    }
    private void bind(string fileName)
    {
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
             "Data Source=" + fileName + ";" +
             "Extended Properties=Excel 8.0;";
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT *  FROM [Sheet1$]", strConn);
        DataSet ds = new DataSet();
        try
        {
            da.Fill(ds);
            dt = ds.Tables[0];
            this.dgv.DataSource = dt;
        }
        catch (Exception err)
        {
            MessageBox.Show("操作失败!" + err.ToString());
        }
    }

    private void simpleButton2_Click(object sender, EventArgs e)
    {         
            if (dgv.Rows.Count > 0)  
            {  
                DataRow dr = null;  
                for (int i = 0; i < dt.Rows.Count; i++)  
                {  
                    dr = dt.Rows[i];  
                    insertToSql(dr);  
                }                 
                MessageBox.Show("导入成功!");  
            }  
            else 
            {  
                MessageBox.Show("没有数据!");  
            }  
        }  
        private void insertToSql(DataRow dr)  
        {  
            //excel表中的列名和数据库中的列名一定要对应  
            string xmlx = dr["XMLX"].ToString();  
            string xmbm = dr["XMXM"].ToString();  
            string xmmc = dr["XMMC"].ToString();  
            string xmdw = dr["XMDW"].ToString();  
             string dydj = dr["DYDJ"].ToString();  
            string zyxf = dr["ZYXF"].ToString();  
            string xmnr = dr["XMNR"].ToString();  
            DateTime kssj = Convert.ToDateTime(dr["KSSJ"].ToString());
            DateTime jssj =  Convert.ToDateTime(dr["JSSJ"].ToString());
            int count=BLL10101.InsertOrUpdate(xmlx,xmbm,xmmc,xmdw,dydj,zyxf,xmnr,kssj,jssj);
        }  
    }  

}

以上是我在网上找的代码,但是有出现错误,求大神们看看。图片说明

1个回答

oledb只能读excel,写入 excel用npoi

qq_23126581
渊渟无迹静涛君 我没有写入excel呢,我是要将excel数据导入数据库呢,昨天有人说我这个把项目平台改成x86,但是我是64位系统
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问