vb中如何连接数据库?
我是个新手,在做一个登陆的界面,在 sql2000 中我建立了一个数据库(里面有表:含字段user,key)在不同的书上面,具体连接数据库的步骤不一样,一直很迷茫,下面是我的想法:
要通过用户输入的信息与数据库中表的字段比对,如果相同就进入主界面,否则报错。代码应该怎么实现。
请高手指点!小弟感激不尽! --------------------编程问答-------------------- Option Explicit
Dim login As Integer
Dim excel_app As Object
Dim excel_sheet As Object
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOK_Click()
Dim excel_app As Object
Dim excel_sheet As Object
Dim row As Integer
Dim n As String
Dim p As String
Dim name As String
Dim pswd As String
login = login + 1
If login > 3 Then
MsgBox "您已达到最大错误次数,系统将退出!", 0 + 48, "提示"
Unload Me
Exit Sub
End If
Set excel_app = CreateObject("Excel.Application")
excel_app.Workbooks.Open FileName:="F:\财务分析系统设计\data\admin.xls"
name = Trim(textUserName.Text)
pswd = Trim(txtPassword.Text)
If Val(excel_app.Application.Version) >= 8 Then
Set excel_sheet = excel_app.ActiveSheet
Else
Set excel_sheet = excel_app
End If
row = 2
n = Trim$(excel_sheet.Cells(row, 2))
p = Trim$(excel_sheet.Cells(row, 3))
If name = n And pswd = p Then
excel_app.ActiveWorkbook.Close False
excel_app.Quit
Set excel_sheet = Nothing
Set excel_app = Nothing
Unload Me
frmmain.Show
ElseIf name <> n Or pswd <> p Then
MsgBox "用户名或密码错误, 请重新输入!", 0 + 48, "提示"
excel_app.ActiveWorkbook.Close False
excel_app.Quit
Set excel_sheet = Nothing
Set excel_app = Nothing
End If
End Sub
连接sql
Private Sub Command2_Click()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "Driver={sql server};server=B23;uid=sa;pwd=;database=cwfx"
conn.ConnectionTimeout = 30
conn.Open
End Sub
改下就可以了
--------------------编程问答-------------------- 楼上代码应该可用..
就是看了头晕..没有层次感 --------------------编程问答-------------------- 有个最简单的办法,用数据库联接控件好像叫adodbc,配置该控件生成联接串,复制该联接串,然后用adodb对象,用该联接串联接就可以了
补充:VB , 数据库(包含打印,安装,报表)