1

I'm trying to import data from an excel sheet into my database, however some data would be imported as NULL. Which includes

1) Something that contains '-'. eg. 12345678-9

2) A number that has a warning on excel that says number stored as text, and when the option convert this to number, it goes from 811123123121014259 to 8.96503E+17

I am importing the data via ASP.NET

using (OleDbConnection excelConnection = new OleDbConnection(conStr))
{
    //Create OleDbCommand to fetch data from Excel 
    using (OleDbCommand cmd = new OleDbCommand("SELECT * From [" + SheetName + "]", excelConnection))
    {
        excelConnection.Open();
        using (OleDbDataReader dReader = cmd.ExecuteReader())
        {
            using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
            {
                //Give your Destination table name 
                sqlBulk.DestinationTableName = "TableName";
                sqlBulk.WriteToServer(dReader);
            }
        }
        excelConnection.Close();
    }
}

1 Answer 1

1

You have to change your connection string.

Use this one when you want to treat all data in the file as text, overriding Excels column type "General" to guess what type of data is in the column.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";

More informations here.

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

3 Comments

It's still imported as NULL
@BloopieBloops check your connection string again. When this happens it's a connection string related problem.
Right now, I'm using this: Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR={1};IMEX=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.