且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据从Excel工作表导入MySQL数据库?

更新时间:2022-10-17 23:07:21

,oledbConn);

//创建新的OleDbDataAdapter
OleDbDataAdapter oleda = new OleDbDataAdapter();

oleda.SelectCommand = cmd;

//创建一个DataSet,它将保存从工作表中提取的数据。
DataSet ds = new DataSet();

//从提取的数据中填充DataSet工作表。
oleda.Fill(ds,Customer);
//这里有更多代码.....
//代码就在这里....


我已经尝试过了。



我可以执行,如果我替换下面的数据源;



此代码

  string  path = FileUploadContacts.PostedFile.FileName; 
string connString = Provider = Microsoft.ACE.OLEDB.12.0;数据源= +路径+ ;扩展属性= Excel 12.0;









 string connString =Provider = Microsoft.ACE.OLEDB.12.0;数据源= C:\\Sample.xlsx;扩展属性= Excel 12.0; 





问题出在FileUpload控件上。可能它没有采用完整的路径。

如何解决这个问题?


Hi,

I have an Excel sheet containing data and i want to upload that to the MYSQL database.
I have used FileUpload control to select the Excel file by the user.
While reading the data from Excel sheet the following error occuring at oledb.Open();

The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.

Below is my code:

 protected void btnImportcontacts_Click(object sender, EventArgs e)
    {
        string path =FileUploadContacts.PostedFile.FileName;
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path+";Extended Properties=Excel 12.0";
        // Create the connection object
        OleDbConnection oledbConn = new OleDbConnection(connString);
        //try
        //{
          
            // Open connection
            oledbConn.Open();
           
 
            // Create OleDbCommand object and select data from worksheet Sheet1
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
 
            // Create new OleDbDataAdapter
            OleDbDataAdapter oleda = new OleDbDataAdapter();
 
            oleda.SelectCommand = cmd;
 
            // Create a DataSet which will hold the data extracted from the worksheet.
            DataSet ds = new DataSet();
 
            // Fill the DataSet from the data extracted from the worksheet.
            oleda.Fill(ds, "Customer");
            // Some more code here.....
           // code goes here....

", oledbConn); // Create new OleDbDataAdapter OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; // Create a DataSet which will hold the data extracted from the worksheet. DataSet ds = new DataSet(); // Fill the DataSet from the data extracted from the worksheet. oleda.Fill(ds, "Customer"); // Some more code here..... // code goes here....


I have already tried.

I am able to execute, if i replace the data source like below;

This code
string path =FileUploadContacts.PostedFile.FileName;
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path+";Extended Properties=Excel 12.0";



as like

string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Sample.xlsx;Extended Properties=Excel 12.0";



The problem is with the FileUpload Control.May be it is not taking the complete Path.
How can i resolve this?