简体   繁体   English

将数据从Excel工作表传输到SQL Server数据库

[英]Transfer data from Excel sheet into SQL Server database

I have an Excel sheet that contains some data. 我有一个包含一些数据的Excel工作表。 I want to store that data into a SQL Server database from that aspx page. 我想将该数据从该aspx页存储到SQL Server数据库中。

I am trying to store data but get error as below: 我正在尝试存储数据,但出现以下错误:

The Microsoft Office Access database engine cannot open or write to the file. Microsoft Office Access数据库引擎无法打开或写入文件。 It is already opened exclusively by another user, or you need permission to view and write its data. 它已经由另一个用户专门打开,或者您需要权限才能查看和写入其数据。

This is my code: 这是我的代码:

string excelConnectionString = @"provider=Microsoft.ACE.OLEDB.12.0;data source=" + path + ";extended properties='Excel 12.0;HDR=YES;'";

//Create Connection to Excel work book
OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]",excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "Excel_table";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();

and thanks in advance ... 并预先感谢...

您需要关闭Access数据库数据库文件,否则代码将访问data /。

Hi check the following things: 嗨,请检查以下内容:

1.Access file which you are using should not be opened. 1.不要打开正在使用的访问文件。

2.It should not be read only. 2.它不应该是只读的。

3.You should have read/write permission for folder where file that you want to access is placed. 3.您应该对要访问的文件所在的文件夹具有读/写权限。

适当的用户帐户(默认情况下,NETWORK SERVICE)需要对Excel文件所在的文件夹具有“修改”权限。将该权限添加到存在Excel文件的那个文件夹中。

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

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