从Excel导入到SQL Server c#

从Excel导入到SQL Server c#,c#,sql-server,excel,sqlbulkcopy,import-from-excel,C#,Sql Server,Excel,Sqlbulkcopy,Import From Excel,我正在尝试将数据从Excel文件导入SQL Server中的表,但它引发了错误 string ssqltable = "mytable"; string myexceldataquery = "select * from [Order Form$]"; try { string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;P

我正在尝试将数据从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;";

Excel连接字符串丢失了最初的双引号“\@”…;扩展属性=\“…\”。我将Excel连接字符串更改为字符串sexcelconnectionstring=@”provider=microsoft.jet.oledb.4.0;数据源=C:\\Users\\usidqui\\Desktop\\myexcel.xlsx;扩展属性==“+”\”Excel 12.0;hdr=是\""; 但仍然是相同的错误检查这个问题:。您的连接字符串不正确。我的sql字符串没有问题,因为在OLEDBConnection之前,我正在从执行正常的sql表中删除数据,我检查了表中的所有数据。与错误无关,但我非常确定您的“while”循环将导致导入跳过第一行。去掉“while”,只需调用bulkcopy.WriteToServer(dr)。