Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Insert Data into SQL Server Database using VBScript

3,746 views
Skip to first unread message

Ernie FErro

unread,
Feb 25, 2005, 11:22:51 AM2/25/05
to
I've been using vbscript to read and parse data out of some text files. Is
there a way I can take this data and insert into a table in my SQL Server
database? Could someone point me in the direction of some code snippets if
it's possible or post something to get me started? Thanks in advance.
Ernie


richard....@adminscripting.co.uk

unread,
Feb 25, 2005, 1:48:10 PM2/25/05
to
Hi,

You could try this to get you started - its a bit over the top as you
do not need a recordset to insert, update or delete data.

'create the connection string - called cn1 in this case with a DSN
called myDSN
set cn1 = wscript.createobject("ADODB.Connection")
cn1.commandtimeout=1000
cn1.open "myDSN"

'create your record set called rs1 in this case
set rs1 = wscript.createobject("ADODB.Recordset")

sql = "Insert into mytable (col1, col2) Values ('va1', 'val2')"
'write the data to the DB
rs1.open sql, cn1


To use the ADODB.Command object I think the below should work:

set cn1 = wscript.createobject("ADODB.Connection")
cn1.commandtimeout=1000
cn1.open "myDSN"
set myComm = wscript.CreateObject("ADODB.Command")
myComm.ActiveConnection = cn1
myComm.CommandText ="Insert into mytable (col1, col2) Values ('va1',
'val2')"
myComm.Execute

R

Richard Gascoigne

Jeff Cochran

unread,
Feb 25, 2005, 4:18:40 PM2/25/05
to

How are you reading the data? With a SQL SELECT query, right? Use an
INSERT query to do the reverse.

Jeff

ebferro

unread,
Feb 25, 2005, 4:50:58 PM2/25/05
to
Jeff:
No, I'm not using a query. The data is not csv or tab separated. I've
been reading it in line by line and programmatically getting the
information that I need. Now, I want to insert it into my table. Given
that the data is not uniform, could I use the SQL Select query? What
would the code to do something like that look like. Thanks in advance,
by the way.
Ernie


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

ebferro

unread,
Feb 25, 2005, 5:15:57 PM2/25/05
to

Richard:
Thanks for the code. I'll give it a try.
0 new messages