我已經創建了一個演示,用於將Excel文件上傳到網絡服務器,然後將數據從它複製到SQL服務器數據庫。它工作完美。從excel傳輸數據到SQL
ASPX設計:
<table>
<tr>
<td>
<span style="color: Red">*</span>Attach Excel file
</td>
<td>
<asp:FileUpload ID="fileuploadExcel" runat="server" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSend" runat="server" Text="Export" onclick="btnSend_Click" />
</td>
</tr>
</table>
後面的代碼:
private String strConnection = "Data Source=Test-PC;Initial Catalog=ExcelMapping;User ID=Test;Password=Test";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
//file upload path
string path = fileuploadExcel.PostedFile.FileName;
fileuploadExcel.SaveAs(Server.MapPath(path));
//Create connection string to Excel work book
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(path) + ";Extended Properties=Excel 12.0;Persist Security Info=False";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select [lat],[long] from [Sheet1$]", excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
DataSet ds = new DataSet();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "Test";
sqlBulk.ColumnMappings.Add(0, 1);
sqlBulk.ColumnMappings.Add(1, 2);
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
}
}
但是,當我使用相同的代碼在現有的應用程序。它給我的錯誤,同時打開與Excel陳述
的「Microsoft.ACE.OLEDB.12.0」供應商未在本地機器上註冊的連接
請建議我做什麼來解決這個。
你說,你有一個樣品溶液的工作同一臺機器? – nunespascal
您是否嘗試過使用ODBC數據源管理器添加該提供程序? – rajeemcariazo
@nunespascal是演示在同一臺機器上工作。 –