• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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.

Thanks,

Bill





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
	Next  
        Wscript.Echo "Finished! Thank You!"

Open in new window

0
bjennings
Asked:
bjennings
  • 2
1 Solution
 
David LeeCommented:
Hi, bjennings.

You need code like that below.  Here's a link to guidance on the connection string for SQL server: http://www.connectionstrings.com/?carrier=sqlserver2005  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

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

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now