'使用方法:1、调用SetConnStr函数,产生连接字符串。2、打开数据库连接,3。执行ExecuteSQL函数返回结果集。 '======================== 必须引用ADO2.5库方可运行 ========================== 'VB数据库编程的模块,演示如何用ADO连接Access数据库,以及 SQL 查询语言的运用。 '引用ADO酷,可以将ADO孔件拖到窗体上再删除就可以了 '============================================================================= Public Function SetConnStr(ByVal DbName As String) As String '生成数据库连接字符串,参数为数据库名(全路径) SetConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbName & ";Persist Security Info=False" End Function Public Function ExecuteSQL(ByVal Sql As String, ByVal ConnString As String) As ADODB.Recordset 'On Error Resume Next'当数据库插入时,全部数据均为空时,将发生错误,所以要加这句话 '执行给定的sql语句,返回类型为记录集 Dim Mycon As ADODB.Connection Dim Rst As ADODB.Recordset Set Mycon = New ADODB.Connection Mycon.ConnectionString = ConnString Mycon.Open Dim Stokens() As String ' On Error GoTo ExectueSql_Error Stokens = Split(Sql) If InStr("INSERT,DELETE,UPDATE", UCase(Stokens(0))) Then Mycon.Execute Sql Else Set Rst = New ADODB.Recordset Rst.Open Trim(Sql), Mycon, adOpenKeyset, adLockOptimistic Set ExecuteSQL = Rst End If ExectueSql_Exit: Set Rst = Nothing Set Mycon = Nothing Exit Function ExectueSql_Error: Resume ExectueSql_Exit End Function
人生如梦梦如烟
2024-06-07 03:36:41
把' " & pastrecord & " '前后的单引号去了。 为:cn.Execute "select * into " & pastrecord & " from oldtable"