?
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
Medium Priority
?
1,363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 38

Assisted Solution

by:Jim P.
Jim P. earned 600 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 1400 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 1400 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

Stack Overflow Podcast - Frustrating Miracles

In this podcast, Stack Overflow interviewed Linux Academy CEO/Founder, Anthony James, and got his developer story!

"Follow your passion, be prepared to work hard and sacrifice, and, above all, don't let anyone limit your dreams."  - Donovan Bailey

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 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