繁体   English   中英

从Excel导入到SQL Server C#

[英]Importing from Excel to SQL Server c#

我正在尝试将数据从Excel文件导入到SQL Server中的表,但是会引发错误。

            string ssqltable = "mytable";

        string myexceldataquery = "select * from [Order Form$]";
        try
        {
            string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;Persist Security Info=False;data source='C:\Users\usiddiqui\Desktop\myexcel.xlsx';extended properties=" + "\'excel 12.0;hdr=yes;\';";
            string ssqlconnectionstring = "server=SANJSQL-DEV;Database=db; User Id=user; Password=pass;";

            string sclearsql = "delete from " + ssqltable;
            SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
            SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
            sqlconn.Open();
            sqlcmd.ExecuteNonQuery();
            sqlconn.Close();
            OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
            OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
            oledbconn.Open();
            OleDbDataReader dr = oledbcmd.ExecuteReader();
            SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
            bulkcopy.DestinationTableName = ssqltable;
            bulkcopy.BatchSize = 100;
            bulkcopy.WriteToServer(dr);
            //while (dr.Read())
            //{
            //    bulkcopy.WriteToServer(dr);
            //}
            dr.Close();
            oledbconn.Close();
            label1.Text = "File imported into sql server."; 
        }
        catch (Exception ex)
        {
            //handle exception 
        }

该错误来自oledbconn.Open(); 这是错误

找不到可安装的ISAM。

您正在混合转义语法。 您使用@切换到多行,但随后使用\\进行转义。 不要逃避\\并删除最后的\\。

string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=C:\Users\usiddiqui\Desktop\myexcel.xlsx;extended properties=excel 12.0;hdr=yes;";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM