1. dim strSql as string strsql=select * from users where name='管理员' rs.open strsql,conn,2,2 if rs.recordcount>0 then if rs!name="管理员" and rs!pass="123" then
msgbox "正确" end if end if 关闭数据连接
2dim strTmp as stringdim strSql as string strsql=select * from users where name='黄化' rs.open strsql,conn,2,2 if rs.recordcount>0 then strtmp=Rs!年龄 end if 关闭数据连接
第一个: 如表(table1): 姓名 | 密码 管理员 123窗体上有两个文本框text1,text2: 语句: txtSQL="select*from table1 where 姓名='"& text1 & "' and 姓名='"& text2 & "'"
3. Dim Sql as string sql="select * from users where [name]='黄化'" rs.open sql,conn,2,2
1. rs.Open "select * from table1 where 用户名='管理员' and 密码='123'", con, adOpenDynamic, adLockOptimistic If rs.EOF Then MsgBox "在" Else MsgBox "不在" End If rs.Close 2.dim Name ,pass as string rs.open "select * from table1 wher 用户名="黄化", con, adOpenDynamic, adLockOptimistic Name = rs(1).Text pass = rs(2).Text rs.Close
3. Dim mrc As ADODB.Recordset txtsql = "select username,password from use where username='" & _ Trim(Text1.Text) & "'" Set mrc = ExecuteSQL(txtsql) '查询用户 If mrc.EOF = True Then MsgBox " 用户名错误!", vbExclamation + vbOKOnly, "警告" Text1.SetFocus Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) Exit Sub End If
Public Function ExecuteSQL(ByVal sql As String) As ADODB.Recordset '执行一条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("INSER,DELETE,UPDATE", UCase(stokens(0))) Then '如果是INSERT、DELETE、UPDATE将不返回记录集 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
3.怎样把一条SQL语句的结果放到一个变量里?
dim strSql as string
strsql=select * from users where name='管理员'
rs.open strsql,conn,2,2
if rs.recordcount>0 then
if rs!name="管理员" and rs!pass="123" then
msgbox "正确" end if
end if
关闭数据连接
strsql=select * from users where name='黄化'
rs.open strsql,conn,2,2
if rs.recordcount>0 then
strtmp=Rs!年龄
end if
关闭数据连接
如表(table1):
姓名 | 密码
管理员 123窗体上有两个文本框text1,text2:
语句:
txtSQL="select*from table1 where 姓名='"& text1 & "' and 姓名='"& text2 & "'"
Dim Sql as string
sql="select * from users where [name]='黄化'"
rs.open sql,conn,2,2
con.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath & ";Persist Security Info=False"
1. rs.Open "select * from table1 where 用户名='管理员' and 密码='123'", con, adOpenDynamic, adLockOptimistic
If rs.EOF Then
MsgBox "在"
Else
MsgBox "不在"
End If
rs.Close
2.dim Name ,pass as string
rs.open "select * from table1 wher 用户名="黄化", con, adOpenDynamic, adLockOptimistic
Name = rs(1).Text
pass = rs(2).Text
rs.Close
3. Dim mrc As ADODB.Recordset
txtsql = "select username,password from use where username='" & _
Trim(Text1.Text) & "'"
Set mrc = ExecuteSQL(txtsql) '查询用户
If mrc.EOF = True Then
MsgBox " 用户名错误!", vbExclamation + vbOKOnly, "警告"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Exit Sub
End If
Public Function ExecuteSQL(ByVal sql As String) As ADODB.Recordset
'执行一条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("INSER,DELETE,UPDATE", UCase(stokens(0))) Then
'如果是INSERT、DELETE、UPDATE将不返回记录集
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