Import from comma delimited text file into SQL Server 2008 R2 table using 2010

I have an error(see attached word file) importing from comma delimited text file into SQL Server 2008 R2 table using 2010.

Here's the code:
Dim FileName As String = "textfile.txt"

        Dim txtConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\network\;Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"""
        Dim cn As New OleDbConnection(txtConn)
        Dim cmd As New OleDbCommand("SELECT * FROM " & FileName, cn)

        Dim objDR As OleDbDataReader

        Dim SQLconn As New SqlConnection()

        Dim ConnString As String = "Data Source=Main;Initial Catalog=Main;Persist Security Info=True;User ID=sa;Password=helloworld"

        SQLconn.ConnectionString = ConnString

        Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
            bulkCopy.DestinationTableName = "dbo.tblImport"

                objDR = cmd.ExecuteReader

            Catch ex As Exception
            End Try
        End Using

Open in new window

Thank you.
Queennie LAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
The error would indicate that your SqlConnection isn't opening (line 17). You should debug the code to see if in fact the connection is open when you call SqlBulkCopy and if it isn't why not.
Kyle AbrahamsSenior .Net DeveloperCommented:
Dim cmd As New OleDbCommand("SELECT * FROM " & FileName, cn)

You're using cn in your cmd . . . not SQLconn.

Either associate SQLconn to the cmd or open cn instead of SQLConn.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Queennie LAuthor Commented:

I added and worked perfectly

       Dim cn As New OleDbConnection(txtConn)

But when inserted to the SQL table is NULL.

I don't why is not inserting txt data to sql table.

Thank you.
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Queennie LAuthor Commented:

cn is to connect to text file

SQLconn is to connect to SQL Server table.

Am I doing it wrong?
Kyle AbrahamsSenior .Net DeveloperCommented:
You were doing it right . . . my comment was before you fixed it.  You needed to add and open both connections.  

as for why you're getting null:

Do you have the file open?  First try seeing if you have anything in the reader before writing.

A good example:
Queennie LAuthor Commented:
No Answer?????
Kyle AbrahamsSenior .Net DeveloperCommented:
Do you have the file open?  Also first try making sure you have info in the reader before doing the bulk update.
Queennie LAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.