您好,最近我一直在尝试开发一个程序,使用sqlbulkcopy将excel数据导入SQL Server数据库,但是它仅复制第一页(如果打印,至少会复制第一页)
string ssqltable = "Test";
string myexceldataquery = "Select [Employee Name],[DOB],[Email],[Mobile] from [Sheet1$]";
try
{
string excelconn = @"provider=microsoft.ACE.OLEDB.12.0;data source=" + excelFilePath +
";extended properties=" + "\"excel 12.0;hdr=yes;\"";
sqlconn.Open();
OleDbConnection oledbconn = new OleDbConnection(excelconn);
OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
oledbconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlconn);
bulkcopy.DestinationTableName = ssqltable;
while (dr.Read())
{
bulkcopy.WriteToServer(dr);
}
dr.Close();
oledbconn.Close();