Public Function viewData(ByVal txtSQL As String, dataGridAll As DataGrid) As Integer
Dim rstData As ADODB.Recordset
Dim result As String
'检索需要的信息
result = ExecuteSQL(txtSQL, rstData, False)
'设置datagrid的数据源
If rstData.RecordCount <> 0 Then
Set dataGridAll.DataSource = rstData
viewData = rstData.RecordCount
Else
MsgBox "还没有数据!", vbOKOnly + vbExclamation, "警告"
viewData = 0
End If
End Function
数据库采用sql2000
'函数名:ExecuteSQL
'说 明:执行SQL语句
'参 数:SQL As String, rst As ADODB.Recordset, Optional enableWrite As Boolean
'返回值:SQL语句执行成功——true,失败——false
' ******************************************************************************
Public Function ExecuteSQL(ByVal SQL As String, rst As ADODB.Recordset, _
Optional enableWrite As Boolean = True) As Boolean
Dim con As New ADODB.Connection
Dim sTokens() As String
On Error GoTo Execute_Error
sTokens = Split(SQL)
Set con = New ADODB.Connection
con.Open ConnectString
Set rst = New ADODB.Recordset
If enableWrite Then
rst.Open Trim$(SQL), con, adOpenStatic, adLockOptimistic
Else
rst.Open Trim$(SQL), con, adOpenStatic, adLockReadOnly
End If
ExecuteSQL = True
Exit Function
Execute_Error:
ExecuteSQL = False
Exit Function
End Function
四季花海