How to add a new record to a sql database using a vbscript

Hello Everyone,

I am working on a computer inventory database for my company.  I have a script that will get the username, computer name, and serial number from any computer and enter into a text file.

Now I recently created a sql database to put all these records into.  I would like to modify this script so instead of saving to a text file, it would add a new record to the computerinventory table in my sql database.  Is there anyway I can do this?

Here is the script I have right now.



Dim objWshShell, objWMIService, colSMBIOS, objSMBIOS,objFSO,objFile
	Dim strUserName, strComputerName, strComputer 
	Const ForAppending = 8
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile("\\myserver\directory1$\myinventory.txt", ForAppending)
	Set objWshShell = CreateObject("WScript.Shell") 
	strUserName = objWshShell.ExpandEnvironmentStrings("%USERNAME	strComputerName = objWshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")   
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
	Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure") 
	For Each objSMBIOS in colSMBIOS 
		 objFile.WriteLine  strUserName& "," & strComputerName& "," & objSMBIOS.SerialNumber
        Wscript.Echo "Finished! Thank You!"

Open in new window

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.

David LeeCommented:
Hi, bjennings.

You need code like that below.  Here's a link to guidance on the connection string for SQL server:  The code in the first block appears once at the top of the script.  The code in the second block replaces line #12 in your code.
'Create the connection to the database'
Set adoCon = CreateObject("ADODB.Connection")
'Change the connection string on the following line'
adoCon.Open "SQL Connection String"
'Insert the data into the database'
'Change the field names on the following line to those of your fields'
strFields = "UserName,ComputerName,SerialNumber"
varValues = "'" & strUserName& "','" & strComputerName& "','" & objSMBIOS.SerialNumber & "'"
adoCon.Execute "INSERT INTO ComputerInventory (" & strFields & ") VALUES(" & varValues & ")"

Open in new window


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
bjenningsAuthor Commented:
Awesome...It worked!  Thank you so much!!!
David LeeCommented:
You're welcome.  Glad I could help.
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
VB Script

From novice to tech pro — start learning today.