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

I have an error(see attached word file) importing from comma delimited text file into SQL Server 2008 R2 table using VB.net 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
        SQLconn.Open()


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

            Try
                objDR = cmd.ExecuteReader
                bulkCopy.WriteToServer(objDR)
                objDR.Close()
                SQLconn.Close()

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Using

Open in new window


Thank you.
Error.docx
Queennie LAsked:
Who is Participating?
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 SuterCommented:
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.
0
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.
0

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:
@Russ:

I added cn.open and worked perfectly

       Dim cn As New OleDbConnection(txtConn)
        cn.Open()

But when inserted to the SQL table is NULL.

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

Thank you.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Queennie LAuthor Commented:
@Kyle:

cn is to connect to text file

SQLconn is to connect to SQL Server table.

Am I doing it wrong?
0
Kyle AbrahamsSenior .Net DeveloperCommented:
You were doing it right . . . my comment was before you fixed it.  You needed to add cn.open 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:
https://msdn.microsoft.com/en-us/library/txy68e2d(v=vs.110).aspx
0
Queennie LAuthor Commented:
No Answer?????
0
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.
0
Queennie LAuthor Commented:
Thank you.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.