Solved

Trying to import specific rows from excel to sql table. But: System.Data.OleDb.OleDbException: No value given for one or more required parameters.

Posted on 2013-11-25
4
1,262 Views
Last Modified: 2013-11-26
Hi,
I have workable code to import excel data into sql table, but only as [Sheet1$]. But, I'm trying to choose specific rows to be imported like [Sheet1$A3:C3]. And, of course not working, and getting an error: System.Data.OleDb.OleDbException: No value given for one or more required parameters.
So, I would like someone to check if this is actual correct procedure, or something else...Or, maybe my approach is totally wrong.

Private strConnection As String = "<MyServerConnection>"
    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
       Dim path As String = fileuploadExcel.PostedFile.FileName
        Dim excelConnectionString As String = (Convert.ToString("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=") & path) + ";Extended Properties=Excel 12.0;Persist Security Info=False"
        Dim excelConnection As New OleDbConnection(excelConnectionString)
        Dim cmd As New OleDbCommand("Select [ID],[FirstName],[SecondName] from [Sheet1$A3:C3]", excelConnection)
        excelConnection.Open()
        Dim dReader As OleDbDataReader
        dReader = cmd.ExecuteReader()
        Dim sqlBulk As New SqlBulkCopy(strConnection)
       sqlBulk.DestinationTableName = "test"
        sqlBulk.WriteToServer(dReader)
        excelConnection.Close()
    End Sub

Thank you in advance for help.
0
Comment
Question by:dejandejanovic
  • 2
4 Comments
 
LVL 38

Assisted Solution

by:Jim P.
Jim P. earned 150 total points
ID: 39676447
Does it take excessive time? Or is there some other reason for wanting to change?

My suggestion would be to import the data to a staging table and then move the rows you want to the final table. You don't state what the DB system you are using in your question. But if it is SQL and you use TRUNCATE TABLE it resets the table each time and shouldn't cause excessive growth.
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 350 total points
ID: 39676509
For me, this works via SQL Server  :

Select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=c:\ee\TEST.xlsx;HDR=NO;IMEX=1', 'SELECT * FROM [Sheet1$A1:C10]') as a         

Open in new window

So, it shouldn't be an oledb problem per se. But you might want to triple check your command line syntax and make sure of the single quotes etc.

But lets also think outside the box...

well, as part of the select statement, you can use the "WHERE" clause.  

the A:C is taken care of by the column names (might want to add in ";HDR=NO;IMEX=1" to make very sure, then columns are F1, F2...etc ) so, it becomes a matter of identifying row=3, and you could select top 3 to make sure it doesn't go further and then only exclude the first two rows in the "where".

Couple of strategies... One is to use the "where" to identify the contents in that row number, next would be to loop and count the rows and only post the three field values (ie don't make SQL read the excel sheet just yet), add in a datatable, load into that and then foreach datarow count rows.

Where and how are you using this ?

There is an example similar to yours in : http://www.mssqltips.com/sqlservertip/2176/dynamically-find-where-table-data-starts-in-excel-using-ssis/
0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 350 total points
ID: 39676532
Ummm, just on reflection, if you are excluding the first row, then how will it be getting its field names ? Try HDR=NO and then use F1,F2,F3 or in the select use F1 as ID, F2 as Firstname etc...
0
 

Author Closing Comment

by:dejandejanovic
ID: 39678833
@Mark_Wills,
OPENROWSET works perfect for my solutions. Thanks again, to all posts.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now