怎么如何用VB软件连接数据库

怎么如何用VB软件连接数据库,第1张

dim

dbConn

As

ADODB.Connection

'定义一个ADODB连接

dim

MenuRs

As

ADODB.Recordset

'定义Recordset对象

dim

WordRs

As

ADODB.Recordset

Function

Conn2DB(byval

dbname

as

string)

As

Boolean

'数据库连函数,dbname为数据库文件的绝对路径

Dim

connStr

As

String

'

Access连接字符串

conStr

=

"Provider=Microsoft.Jet.OLEDB.4.0Data

Source="

&

dbName

&

"Persist

Security

Info=FalseJet

OLEDB:Database

Password=abcdef"

Set

dbConn

=

New

ADODB.Connection

'定义一个ADODB连接对象

If

dbConn.State

<>

adStateOpen

Then

'判断ADODB连接是否打开

dbConn.Open

conStr

'如果不是打开状态

就打开连接

End

If

Conn2DB

=

True

End

Function

Function

CloseDb()

As

Boolean

If

dbConn.State

=

adStateOpen

Then

dbConn.Close:

Set

dbConn

=

Nothing

End

If

End

Function

Private

Sub

Form_Load()

dim

dbname

as

string

dbname="D:\db.mdb"

‘数据库文件路径

if

Conn2DB(dbname)=true

then

msgbox

"连接数据库成功!"

CloseDb

end

if

End

sub

界面设计:

1.ComboBox 名为: CboName 用来输入或显示用户名

2.TextBox 名为:TxtPassword 用来输入密码

3.TextBox 名为:TxtPasswordSure 在注册时用来输入确认密码

4.CheckBox 名为:ChkNew 用来新注册 Caption属性:新建用户

5.CommandButton 名为:CmdLoad 用来登陆或注册 Caption属性:登陆

6.CommandButton 名为:CmdExit 用以退出 Caption 属性:退出

7.Data 不需要多设置,这个控件不用的,只是第一次加载时起作用.Visible属性:False

代码如下:

Dim MyTable As TableDef

Dim MyField As Field

Dim MyDatabase As Database

Dim Myrs As Recordset

Dim StrRule As String

Private Sub CboName_Change()

If Len(CboName.Text) >8 Then

CboName.SelStart = 0

CboName.SelLength = 8

CboName.Text = CboName.SelText

CboName.SelStart = 8

End If

End Sub

Private Sub ChkNew_Click()

If ChkNew.Value = 0 Then

LblPasswordsure.Visible = False

TxtPasswordSure.Visible = False

CmdLoad.Caption = "登陆"

Else

LblPasswordsure.Visible = True

TxtPasswordSure.Visible = True

CmdLoad.Caption = "注册"

End If

CboName.SetFocus

End Sub

Private Sub CmdExit_Click()

End

End Sub

Private Sub CmdLoad_Click()

If CboName.Text = "" Then

MsgBox "请输入用户名!", vbExclamation, "友情提示"

CboName.SetFocus

Exit Sub

ElseIf TxtPassword.Text = "" Then

MsgBox "请输入密码!", vbExclamation, "友情提示"

TxtPassword.SetFocus

Exit Sub

ElseIf TxtPasswordSure.Text = "" And TxtPasswordSure.Visible = True Then

MsgBox "请输入确认密码!", vbExclamation, "友情提示"

TxtPasswordSure.SetFocus

Exit Sub

End If

If CmdLoad.Caption = "注册" Then

If TxtPasswordSure.Text <>TxtPassword.Text Then

MsgBox "密码和确认密码不同!", vbExclamation, "Sorry"

TxtPasswordSure.SetFocus

Exit Sub

End If

If CheckStr(CboName.Text) Then

MsgBox "用户名可以是中文,也可以是英文或数字,但其中不能带有" &vbNewLine &"像“" &StrRule &"”的字符!", vbExclamation, "友情提示"

CboName.SetFocus

Exit Sub

ElseIf CboName.Text = "user" Then

MsgBox "user是保留用字,不能用作用户名!", vbExclamation, "Sorry"

Exit Sub

End If

Set MyDatabase = Workspaces(0).OpenDatabase(App.Path &"\save")

Set Myrs = MyDatabase.OpenRecordset("select * from user where Name = " &Chr(34) &CboName.Text &Chr(34))

If Myrs.RecordCount >0 Then

MsgBox "用户已经存在", vbExclamation, "友情提示"

CboName.SetFocus

Exit Sub

End If

Set Myrs = MyDatabase.OpenRecordset("select * from user")

Myrs.AddNew

Myrs.Fields("name") = CboName.Text

Myrs.Fields("password") = TxtPassword.Text

Myrs.Fields("logontime") = Now

Myrs.Fields("Best") = "0"

Myrs.Fields("Run") = "0"

Myrs.Update

Myrs.Close

Set MyTable = MyDatabase.CreateTableDef(CboName.Text)

Set MyField = MyTable.CreateField("Result", 10, 4)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("RightRatio", 10, 6)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("TestTime", 10, 19)

MyTable.Fields.Append MyField

MyDatabase.TableDefs.Append MyTable

MsgBox "注册成功!", vbExclamation, "恭喜你"

CboName.AddItem (CboName.Text)

ChkNew.Value = 0

MyDatabase.Close

Else '登陆

If TxtPassword.Text <>TxtPasswordSure.Text And TxtPasswordSure.Visible = True Then

MsgBox "密码与确认密码不同,请重新输入!", vbExclamation, "友情提示"

TxtPassword.SetFocus

Exit Sub

End If

Set MyDatabase = Workspaces(0).OpenDatabase(App.Path &"\save")

Set Myrs = MyDatabase.OpenRecordset("select * from user where name =" &Chr(34) &CboName.Text &Chr(34))

If Myrs.RecordCount = 0 Then

MsgBox "用户不存在!", vbExclamation, "友情提示"

Exit Sub

End If

Myrs.MoveFirst

If TxtPassword.Text = Myrs.Fields("password") Then '登陆成功

Myrs.Close

Set Myrs = MyDatabase.OpenRecordset("select * from user")

If Myrs.BOF = False Then Myrs.MoveLast

If Myrs.BOF = False Then Myrs.MoveFirst

Myrs.MoveFirst

For i = 0 To Myrs.RecordCount - 1

Myrs.Edit

If Myrs.Fields("Name") = CboName.Text Then

Myrs.Fields("Run") = "1"

Else

Myrs.Fields("Run") = "0"

End If

Myrs.Update

Myrs.MoveNext

Next

'登陆成功,你加入要执行的 *** 作.

Unload Me'登陆窗口卸载

Else

Static WrongTime As Integer

WrongTime = WrongTime + 1

If WrongTime = 2 Then End

MsgBox "密码错误!", vbExclamation, "友情提示"

End If

Myrs.Close

MyDatabase.Close

End If

End Sub

Private Sub Form_Activate()

StrRule = "`~!@#$%^&*()_-+=|\:<,>.?/" &Chr(34) &Chr(39)

If Dir(App.Path &"\save.mdb", vbHidden) = "" Then '数据文件不见了

Set MyDatabase = CreateDatabase(App.Path &"\save", dbLangGeneral)

Set MyTable = MyDatabase.CreateTableDef("user")

Set MyField = MyTable.CreateField("Name", 10, 16)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("Password", 10, 6)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("LogonTime", 10, 19)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("Best", 10, 4)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("Run", 10, 1)

MyTable.Fields.Append MyField

MyDatabase.TableDefs.Append MyTable

ChkNew.Value = 2

Else

Set MyDatabase = Workspaces(0).OpenDatabase(App.Path &"\save")

Set Myrs = MyDatabase.OpenRecordset("select * from user")

If Myrs.EOF = False Then Myrs.MoveLast

If Myrs.BOF = False Then Myrs.MoveFirst

Dim i As Integer

Dim ShowIndex As Integer

For i = 0 To Myrs.RecordCount - 1

CboName.AddItem (Myrs.Fields("Name"))

If Myrs.Fields("Run") = "1" Then ShowIndex = i

Myrs.MoveNext

Next

If CboName.ListCount >0 Then

CboName.ListIndex = ShowIndex

Else

ChkNew.Value = 2

End If

Myrs.Close

MyDatabase.Close

End If

TxtPassword.SetFocus

End Sub

Private Function CheckStr(StrTheword As String) As Boolean

Dim i As Integer

For i = 1 To Len(StrRule)

If InStr(1, StrTheword, Mid(StrRule, i, 1)) <>0 Then '含有这样的字符

CheckStr = True

Exit Function

End If

Next

CheckStr = False

End Function

'你要源文件可以发EMail给我索要.

'我的电邮地址是:wangaochao@163.com

新建工程时选数据工程,此时VB6集成调试环境左边工具箱内已加载了有关数据库编程必须的控件。

然后在FORM1窗体中添加ADODC控件和DATAGRID控件,将DATAGRID1的属性DATASOURCE选ADODC1,打开ADODC1控件属性页使用连接字符串,选生成,在提供者选项中选MICROSOFT jet 4.0 OLE DB Provider,然后按要求连接数据库等。在ADODC1控件属性页使用连接字符串空白文本窗口中就有一长串字符串,注意该字符串可复制到程序代码用于编程。ADODC1控件属性页的数据源内有命令文本(SQL)编写窗口可编写SQL查询语言。该窗口的SQL语句可复制到程序代码用于编程。

SQL查询语言主要结构为:

Select 查询字段 from 表名 Where 查询条件语句 [排序语句或分组语句]

查询字段必须分别用(西文)逗号分开或就用一个*号代替,上述查询中排序语句建议最好应用。

SQL查询语言如有错程序运行时告诉你出错,作相应改动即可。本人一般先按上述连接,SQL用"SELECT * FROM TabelName"作调试,无问题,用一个按钮控件将前述需复制的连接字符串和SQL查询语言先复制于按钮控件的CLICK事件中备着,以便放着以后使用。然后删去ADODC1控件,再添加ADODC1控件再添其他内容。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10014829.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存