刘金玉的零基础VB教程083期:
mshflexgrid数据表格绑定数据库
ADODC在哪里?
课堂总结
1、掌握使用adodc控件方法来生成数据库连接字符串
2、在使用数据库前必须要连接数据库conn.open,使用conn.state来判断是否连接数据库,值为0的时候代表没有连接
3、注意在使用数据集rs之前必须要分配内存空间
4、绑定查询前,需要使用sql语句将数据查询出来绑定到rs中
5、使用Set MSHFlexGrid1.DataSource = rs来绑定数据集到mshflexgrid控件上面
6、显示出来后,即绑定后,需要修改表头
文件列表:
界面:
源代码:
Option Explicit Dim sql As String Dim conn As New ADODB.Connection Dim rs As ADODB.Recordset Private Sub Command1_Click() If conn.State = 0 Then conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database3.mdb;Persist Security Info=False" conn.Open End If sql = "insert into [users]([username],[password]) values('" & Text1.Text & "','" & Text2.Text & "')" Set rs = New ADODB.Recordset rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic 'MsgBox "录入成功!" sql = "select * from users" rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic Set MSHFlexGrid1.DataSource = rs '表头 MSHFlexGrid1.TextMatrix(0, 0) = "唯一编号" MSHFlexGrid1.TextMatrix(0, 1) = "账号" MSHFlexGrid1.TextMatrix(0, 2) = "密码" End Sub Private Sub Command2_Click() If conn.State = 0 Then conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database3.mdb;Persist Security Info=False" conn.Open End If Set rs = New ADODB.Recordset sql = "select * from users" rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic Set MSHFlexGrid1.DataSource = rs '表头 MSHFlexGrid1.TextMatrix(0, 0) = "唯一编号" MSHFlexGrid1.TextMatrix(0, 1) = "账号" MSHFlexGrid1.TextMatrix(0, 2) = "密码" End Sub