'准备工作: 数据库 D:\Mydb.mdb 数据库中,建数据表:UserInfo,数据表中,建字段:User,Password,并添加一条记录
'点工程->引用 选中 Microsoft Activex Data Objects 2.1 Library
'控件:添加一个文本框,一个命令按钮
Private Sub Command1_Click()
Dim s As String
Dim Conn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0Data Source=D:\mydb.mdb"
'请将数据库名及路径换成你的实际数据库名及路径
Dim UserName As String
Dim Password As String
s = Text1.Text
If s = "1" Or s = "2" Then
UserName = InputBox("请输入用户名")
Password = InputBox("请输入用户密码")
sql = "Select * From [UserInfo] where User='" &UserName &"'"
'UserInfo请换成你的实际数据表名
Rs.Open sql, Conn, 1, 3
If Rs.EOF Then
MsgBox "没有找到此用户"
Else
If Rs("Password") = Password Then
If s = "1" Then
MsgBox "123"
ElseIf s = "2" Then
MsgBox "456"
End If
Else
MsgBox "密码错误"
End If
End If
End If
End Sub
上面的例子实现了查询
如果是要添加,删除,更新等 *** 作,只要编写相应的SQL语句,再用
Conn.Execute SQL 就可以了,如:
添加的:
SQL="Insert Into [UserInfo](User,Password) Values('aaaa','1234')"
Conn.Execute SQL '执行后,就添加了一条记录
删除:
SQL="Delete From [UserInfo] Where User='aaaa'"
Conn.Execute SQL '删除用户名为aaaa的用户记录
更新:
SQL="Update [UserInfo] Set Password='abcdefg' Where User='aaaa'"
Conn.Execute SQL '执行后,修改用户aaaa的密码为abcdefg
以上只是示例,在实际使用过程中,可以将一些内容用控件输入等方式进行,这样就有很大的灵活性了
动态创建数据库
引用 microsoft DAP 3.6 Object Library
Dim myDB As DAO.Database
Set myDB = CreateDatabase(App.Path + "\111.mdb", dbLangGeneral) '如果不存在数据库
Set myDB = OpenDatabase(App.Path + "\111.mdb") '如果存在数据库
Dim str_SQL As String
str_SQL = "Create Table NewTable1(Field1 Text(10),Field2 Short)"
myDB.Execute str_SQL
str_SQL = "Create Table NewTable2(Field1 Text(10),Field2 Short)"
myDB.Execute str_SQL
myDB.Close
向已经建好的数据库中添加记录
建立数据库a,表名字b,字段c,文本格式,在窗体上画一command ,画一个文本框,这个代码就是将文本内容写入数据库
要先引用microsoft activeX data object 2.5 library
代码如下:
Private Sub Command1_Click()
Dim cnDk As New ADODB.Connection
Dim strDk As String
Dim rDk As New ADODB.Recordset
Dim sql As String
strDk = "DBQ=" &App.Path &"\a.mdbDriver={Microsoft Access Driver (*.mdb)}"
cnDk.Open strDk
sql = "insert into b (c) values ('" &text1.text&"')"
cnDk.Execute sql
cnDk.Close
Set cnDk = Nothing
End Sub
在数据库中添加多条记录
只要可以进行输入文本的都可以
改一下这些吧,你单击一次就提示输入一次,输完了自动保存
改后
Data1.Recordset.AddNew
Data1.Recordset.Fields("username") = trim(inputbox("请输入名字"))
Data1.Recordset("password") =val(trim(inputbox("请输入电话")))
Data1.UpdateRecord
——————————————————————————————
Data1.Recordset.AddNew
Data1.Recordset.Fields("username") = "张三|"
Data1.Recordset("password") = "fsdfsd"
Data1.UpdateRecord
Data1.Recordset.AddNew
Data1.Recordset.Fields("username") = "李四|"
Data1.Recordset("password") = "fsdf432d"
Data1.UpdateRecord
Data1.Recordset.AddNew
Data1.Recordset.Fields("username") = "王五|"
Data1.Recordset("password") = "f42342d"
Data1.UpdateRecord
MsgBox "adad"
========================================================================================
vb+access 数据库的记录的添加与删除
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command2_Click()
Set rs = cn.Execute("select * from wenzhang where 内容='" &Text1.Text &"'")
If Text1.Text = "" Then
MsgBox ("文章不能为空!")
ElseIf rs.EOF = False Then
MsgBox ("文章不能重复!")
Else
cn.Execute ("insert into wenzhang values('" &Combo1.Text &"','" &Text3.Text &"','" &Text4.Text &"','" &
Text1.Text &"')")
MsgBox ("ok")
End If
End Sub
'添加按钮
Private Sub Command3_Click()
Dim myval As String
Set rs = cn.Execute("select * from wenzhang where 内容='" &Text1.Text &"'")
If rs.EOF Then
myval = MsgBox("是否保存文章?", vbInformation + vbYesNoCancel, "提示")
If myval = vbYes Then
cn.Execute ("insert into wenzhang values('" &Combo1.Text &"','" &Text3.Text &"','" &Text4.Text &"','" &
Text1.Text &"')")
MsgBox ("保存成功")
Else
Combo1.Text = ""
End If
Else
Text1.Text = ""
Combo1.Text = ""
End If
End Sub
'删除按钮
Private Sub Command4_Click()
If Text1.Text = "" Then
MsgBox ("不能删除空记录!")
Else
Dim myval As String
myval = MsgBox("是否删除文章?", vbInformation + vbYesNo, "提示")
If myval = vbYes Then
Set rs = cn.Execute("delete from wenzhang where 内容 ='" &Text1.Text &"'")
MsgBox ("删除成功!")
End If
End If
End Sub
'连接数据库
Private Sub Form_Load()
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=\vb.net\temp.mdbPersist Security Info=False"
cn.CursorLocation = adUseClient
cn.Open
End Sub
楼主多看看sql语言。我这儿有个范例:
Private Conn As New ADODB.Connection
Private Recordset As New ADODB.Recordset
Private Sub Command1_Click() '增加记录
Dim sql As String
Set Recordset = Nothing
sql = "select * from liuyan" 'SQL查询语句
Recordset.Open sql, Conn, 1, 3
Recordset.AddNew
Recordset("姓名") = Text1.Text '数据读写 *** 作
Recordset("性别") = Text2.Text '数据读写 *** 作
Recordset("年龄") = Text3.Text '数据读写 *** 作
Recordset("权限") = Text4.Text '数据读写 *** 作
Recordset("时间") = Text5.Text '数据读写 *** 作
Recordset("内容") = Text6.Text '数据读写 *** 作
Recordset.Update
Call Command3_Click '刷新
Recordset.Close
End Sub
Private Sub Command3_Click() '显示liuyan表中内容
Dim sql As String, tex As String
Set Recordset = Nothing
sql = "select * from liuyan" 'SQL查询语句
Recordset.Open sql, Conn, 1, 3
Do While Not Recordset.EOF
For i = 1 To Recordset.Fields.Count - 1
If i = Recordset.Fields.Count - 1 Then
tex = tex &Recordset.Fields(i).Name &":" &Recordset.Fields(i).Value &vbNewLine
Else
tex = tex &Recordset.Fields(i).Name &":" &Recordset.Fields(i).Value &Space(15 - Len(Recordset.Fields(i).Value))
End If'输出jsb表中各字段
Next i
Recordset.MoveNext
Loop
'Print Recordset.Fields(2).Type
Text7.Text = tex
End Sub
Private Sub Form_Load()
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0Data Source=G:\实验平台\数据库\留言板.mdb"
End Sub
1、打开代码窗口,添加引用:Imports System.Data.SqlClient。
2、输入以下代码:
“Public conn1 As SqlConnection = New SqlConnection _
("server=192.168.1.79Initial Catalog= studentUser ID= panqePWD=shentai768@")”,vb就已经成功连接sql数据库了。
3、代码详解:声明关键字Public(因为是全局变量,所以用Public 来声明)。
4、连接参数。
5、如果SQL 数据库就在本机,则用以下代码连接:
("server=.Integrated Security=FalseInitial Catalog= studentUser ID= panqePWD=shentai768@")。
6:如果代码太长,影响可读性,可以用空格加"_"后,回车换行。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)