Connect to a databaseTag(s): WSH VBScript


[odbcselect.vbs]
Dim OdbcDSN
Dim connect, sql, resultSet

OdbcDSN = "DSN=Sybase Demo DB V6 DWB;UID=dba;PWD=sql"
Set connect = CreateObject("ADODB.Connection")
connect.Open OdbcDSN

sql="SELECT emp_fname, emp_lname FROM employee"

Set resultSet = connect.Execute(sql)
On Error Resume Next
resultSet.MoveFirst
Do While Not resultSet.eof
  WScript.Echo resultSet("emp_lname") & " , " & _
      resultSet("emp_fname")
  resultSet.MoveNext
Loop

resultSet.Close
connect.Close
Set connect = Nothing

WScript.Quit(0)
[odbcupdate.vbs]
'---- CursorTypeEnum Values ----
Const adOpenForwardOnly = 0
Const adOpenKeyset = 1
Const adOpenDynamic = 2
Const adOpenStatic = 3

'---- LockTypeEnum Values ----
Const adLockReadOnly = 1
Const adLockPessimistic = 2
Const adLockOptimistic = 3
Const adLockBatchOptimistic = 4

Dim OdbcDSN
Dim connect, resultSet

OdbcDSN = "DSN=Sybase Demo DB V6 DWB;UID=dba;PWD=sql"
Set connect = CreateObject("ADODB.Connection")
connect.Open OdbcDSN

Set resultSet = CreateObject("ADODB.Recordset")
Set resultSet.ActiveConnection = connect
resultSet.Source = _
   "SELECT dept_id, dept_name, dept_head_id FROM department WHERE 1=2"
resultSet.CursorType = adOpenStatic
resultSet.LockType = adLockOptimistic
resultSet.Open

On Error Resume Next

resultSet.Addnew
resultSet("dept_id") = 1234
resultSet("dept_name") = "VBScript How-to"
resultSet("dept_head_id") = 501

resultSet.Update

resultSet.Close
connect.Close
Set resultSet = Nothing
Set connect = Nothing

WScript.Quit(0)
Thanks to E. Girardet for this tip
blog comments powered by Disqus