Wrox Programmer Forums
|
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 18th, 2009, 02:15 PM
Registered User
 
Join Date: Mar 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Bulk Insert Function

I am trying to read a tab delimitted text file into SQL Server 2008 Express Database and have those data display on a data grid on a form window that I created. I have heard about the bulk insert or bulk copy method for doing this but I have yet to find the syntax for it.

Also I need to skip lines in the text file before data is recorded into the database. Any help would be greatly appreciated.

Code is as follows:

Dim AllText AsString = "", LineOfText AsString = ""
OpenFileDialog1.Filter = "Text files (*.TXT)|*.TXT"
OpenFileDialog1.ShowDialog() 'display Open dialog box
If OpenFileDialog1.FileName <> ""Then
Try'open file and trap any errors using handler
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
DoUntil EOF(1) 'read lines from file
LineOfText = LineInput(1)
'add each line to the AllText variable
AllText = AllText & LineOfText & vbCrLf
Loop
(import data here with proper formatting to data base)
Catch
MsgBox("Error opening file.")
Finally
FileClose(1) 'close file
Me.DataGridView1.Update()
EndTry
EndIf
 
Old March 18th, 2009, 02:54 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hello.. So, what's your problem???

Bulk insert is an SQL directive to do an insert from a file. You need an schema file for the file and the file itself. It work very fast, but I don't know if you can make it work from outside SQL management (altough you are free to try, I never try it).

Also you know you have pure .net object to open, read and manage a file (you are using vb6 compatiblity objects, no good if you are using vb2008). read about the system.io namespace.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old March 18th, 2009, 04:27 PM
Registered User
 
Join Date: Mar 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I guess what I'm really trying to do is automate this procedure into one event, i.e. when the user opens the file, then file is imported into the database and "magically" it will be in the proper format in the correct columns and rows. I am new to VB 2008 express and the syntax for this eludes me...this is what I need help.
 
Old March 18th, 2009, 05:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

And that's why microsoft invent MSDN... :) read the whole bunch and if you need specific help just ask. Take in mind that you need the to know the format in advance (you have to supply a format file for it). If the format will change, you will have to make a formated import proccess (complicate, but not imposible, it only takes time to build).
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Bulk Insert snufse SQL Server 2000 9 December 17th, 2007 07:53 AM
Bulk Insert question iacon SQL Server 2005 2 October 2nd, 2007 06:51 PM
bulk insert hymavathy_kr VB Databases Basics 1 May 17th, 2007 07:47 AM
Bulk Insert luma SQL Server DTS 1 July 13th, 2005 01:48 AM
Bulk insert deyakhatib SQL Server 2000 6 March 4th, 2004 04:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.