Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
muffazal
Starting Member
1 Post |
Posted - 2012-08-15 : 01:33:02
|
HiI am designing a web application in ASP.NET using VB and would like to import data from Excel (.xls) to SQL Server 2008 using OpenDataSource function. I have designed a form, from where the user clicks the Browse button and locates the Excel file to be imported.The code I have written is pasted below for your reference. The code works fine if the DATA SOURCE is hard coded and imports the data from Excel to SQL Server. But when I specify a variable name in DATA SOURCE it gave me the following error:ERROR: The OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" does not contain the table "Data1$". The table either does not exist or the current user does not have permissions on that table.Since the user will be browsing for the Excel file which can be located anywhere on the machine, I want the DATA SOURCE value to be a variable. Any help regarding this will be greatly appreciated.--------------------------------------------------------------------------------------------------------------------------------------------NOTE:"fileimport" is a FILEUPLOAD Control"filepath" is a variable where the absolute path of the excel file to be imported will be storedEXCEL FILE IMPORT CODE:Protected Sub txtimport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles txtimport.ClickIf fileimport.HasFile ThenDim fileextension As StringDim filepath As Stringfileextension = System.IO.Path.GetExtension(fileimport.FileName)filepath = Server.MapPath(fileimport.FileName)If (fileextension = ".xls") Or (fileextension = ".XLS") ThenTryDim con As SqlConnectionDim cmd As SqlCommandDim dtr As SqlDataReaderDim importquery As Stringcon = New SqlConnection("Server=localhost;Integrated Security = true;Database=RTS")con.Open()importquery = "SELECT * INTO ABC FROM " & _"OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', " & _"'Data Source=" & filepath & ";" & _"Extended Properties=Excel 8.0')...[Data1$]"cmd = New SqlCommand(importquery, con)dtr = cmd.ExecuteReadercon.Close()dtr.Close()Catch ex As Exceptionlblmsg.Text = "Error: " & ex.Message.ToString()End TryElselblmsg.Text = "Wrong File Extension"End IfElselblmsg.Text = "You have not selected a file"End IfEnd Sub--------------------------------------------------------------------------------------------------------------------------------------------Kind RegardsMuffazal Tikiwala |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-08-15 : 07:30:26
|
Although in OPENDATASOURCE you cannot use variables in the arguments, what you are doing should work, because you are simply providing a string constant. You may already have done this, but if not: Just before the statement shown below, print out or examine via debugger whether the variable importquery contains exactly the same string in the case where it works and in the case where it does not work: cmd = New SqlCommand(importquery, con) If it does, I don't see any reason why it would work in one case and wouldn't in another. |
|
|
|
|
|
|
|