VBS kết nối SQL server:
Public cn, rs, cmd, par
Sub ConnectDataBase()
'On Error Resume Next
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set cmd =CreateObject("ADODB.Command")
Set par= CreateObject("ADODB.Parameter")
If cn.State = 1 Then
'cn.Close
Exit Sub
End If
cn.ConnectionString = "Provider=SQLOLEDB.1;User ID=sa; Password=sa; Persist Security Info=False; Initial Catalog=Databasename;Data Source=.\WINCC"
cn.open
End Sub
'Ham Execute SQL, co the tra ve Recordset hoac chi thuc thi CSDL
Function SQLExecute(Byval strSQL, Byval res)
On Error Resume Next
SQLExecute = False
If Not res Is Nothing Then
'Set res = cn.Execute(strSQL)
res.Open strSQL, cn, 1, 3
Else
cn.Execute strSQL
End If
SQLExecute = True
End Function
Sub CloseDatabase()
On Error Resume Next
If cn.State = 1 Then
cn.Close
Set cn = Nothing
End If
End Sub
Public cn, rs, cmd, par
Sub ConnectDataBase()
'On Error Resume Next
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set cmd =CreateObject("ADODB.Command")
Set par= CreateObject("ADODB.Parameter")
If cn.State = 1 Then
'cn.Close
Exit Sub
End If
cn.ConnectionString = "Provider=SQLOLEDB.1;User ID=sa; Password=sa; Persist Security Info=False; Initial Catalog=Databasename;Data Source=.\WINCC"
cn.open
End Sub
'Ham Execute SQL, co the tra ve Recordset hoac chi thuc thi CSDL
Function SQLExecute(Byval strSQL, Byval res)
On Error Resume Next
SQLExecute = False
If Not res Is Nothing Then
'Set res = cn.Execute(strSQL)
res.Open strSQL, cn, 1, 3
Else
cn.Execute strSQL
End If
SQLExecute = True
End Function
Sub CloseDatabase()
On Error Resume Next
If cn.State = 1 Then
cn.Close
Set cn = Nothing
End If
End Sub
Comment