1

I am importing data from Excel into a SQL Server database using vb.net. In my Excel file, in column bNumber, there are values of different types i.e some are numbers and some are text:

Telenorx Telenorx 8 97150219924 97150219924 97150219924 97150219924 Easypayx 92 

When I select the data from Excel through OleDbCommand, it retrieves numbers correctly but text values as blank.

In Excel, the column's data type is General.

This is my code to retrieve data from Excel.

excelConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + OpenFileDialog1.FileName + ";Extended Properties=""Excel 12.0 Xml;HDR=Yes""") Dim oleDbCommand As OleDbCommand = New OleDbCommand("Select bNumber from [Sheet1$]", excelConn) excelConn.open() Dim dataReader = oleDbCommand.ExecuteReader dataReader.read() 
4
  • What is your SQL Datatype ? Commented May 31, 2017 at 13:53
  • Can you provide some details of what you are doing here? How are you querying the data? Give us something. Commented May 31, 2017 at 14:03
  • If you have a problem with your code, you should post said code. It's impossible to help except to say that you have a bug somewhere Commented May 31, 2017 at 14:14
  • I have updated my question. Commented Jun 1, 2017 at 6:44

1 Answer 1

1

This is not necessarily a solution for your case yet would I did was created a SQL-Server table with bNumber as nvarchar, used SQL-Server Management Studio to export to Excel where I placed the Excel file in the bin\Debug folder of the project. Using the code below all rows returned properly (as we have a string column strings are returned).

The key here is using IMEX=1 in the connection string, Excel can be finicky, this may or may not resolve the issue.

Imports System.Data.OleDb Public Class Operations Public Function GetData(ByVal FileName As String) As List(Of String) Dim valueList As New List(Of String) Using cn As New OleDbConnection With { .ConnectionString = ConnectionString(FileName) } Using cmd As OleDbCommand = New OleDbCommand("SELECT bNumber FROM [Table_1$]", cn) cn.Open() Dim reader As OleDbDataReader = cmd.ExecuteReader While reader.Read valueList.Add(reader.GetString(0)) End While End Using End Using Return valueList End Function Public Function ConnectionString(ByVal FileName As String) As String Dim Builder As New OleDbConnectionStringBuilder If IO.Path.GetExtension(FileName).ToUpper = ".XLS" Then Builder.Provider = "Microsoft.Jet.OLEDB.4.0" Builder.Add("Extended Properties", "Excel 8.0;IMEX=1;HDR=Yes;") Else Builder.Provider = "Microsoft.ACE.OLEDB.12.0" Builder.Add("Extended Properties", "Excel 12.0;IMEX=1;HDR=Yes;") End If Builder.DataSource = FileName Return Builder.ConnectionString End Function End Class 

Form code

Private Sub Button3_Click(sender As Object, e As EventArgs) _ Handles Button3.Click Dim ops As New Operations Dim fileName As String = IO.Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Downloaded.xlsx") Dim valueList As List(Of String) = ops.GetData(fileName) For Each value As String In valueList Console.WriteLine(value) Next End Sub 

Results in the IDE Output window

enter image description here

Table structure

enter image description here

Worksheet

enter image description here

EDIT: The following link points to a MSDN code sample for working with SpreadSheetLight library. Once downloaded, set startup form to StackOverFlow1QuestionMixedTypesForm. Right click on the solution in solution explorer and select "Restore NuGet Packages", now build and run the project. There are two buttons on the form, first does mixed types for Sheet1 while button two is a variant of the same data laid out differently in Sheet2.

Code sample https://code.msdn.microsoft.com/Alternate-methods-to-work-4c52c4a2

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your efforts and concern. you just type the values in excel which i provided but this is not the case. My problem is in excel file because the column type is 'General'. if i select the data as it as then my query is unable to retrieve 'Text' as General and if i change the column type to 'Text' then all data can be fetched but numbers become like this. 9.23495E+11, 9.23495E+11, 9.23494E+11. Also i don't want users to bother about changing excel column type.
If open to an alternate method, there is a free library that might be helpful called SpreadSheetLight. This library offers more freedom when reading Excel data. Downside is all examples are in C# on their site yet this week created a code sample on MSDN that shows the majority of common operations and to boot, this morning created an example that would give you a running start that I can append to my first reply, just let me know (it's in VB.NET)
yes why not, I will appreciate it and i can also code in c#. Just keep it in consideration that my excel files have more then one columns of different data types and i don't want the user of my app to change the column' s data type because i think it will be inconvenient for user to open excel file, change the data type of column and then come to my app and upload the file. it is also to be noted that i m receiving excel files from outside and app user has just to select the file and upload data.
Just updated my reply, refer to the last section of my reply.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.