各位大侠指教。关于vb链接数据库,用listview来显示,点击按钮,向数据库添加数据,listview同步显示出来
问题在标题已经说了,下面是编码先用access建立了“试验库”试验表了
然后引用了ado对象了
接着添加了一个text控件,添加按钮,还有listview1.。。求指教,感激不尽。
程序代码:
Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Private Sub Command1_Click() Dim itmx As ListItem Dim cnn As New ADODB.Connection Dim rs As New ADODB.Recordset cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\sk\试验库.mdb;Persist Security Info=False" cnn.Open rs.Open "select * from 试验表", cnn, adOpenKeyset, adLockBatchOptimistic rs.AddNew rs.Fields("名称").Value = Text1.Text rs.Update rs.Close End Sub Private Sub Form_Load() Dim cnn As New ADODB.Connection Dim rs As New ADODB.Recordset cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\sk\试验库.mdb;Persist Security Info=False" cnn.Open rs.Open "select * from 试验表", cnn ListView1.GridLines = True ListView1.View = lvwReport Dim listx As ListItem Dim listsubx As ListSubItem Dim columnx As ColumnHeader Dim i As Integer For i = 0 To rs.Fields.Count - 1 Set columnx = ListView1.ColumnHeaders.Add columnx.Text = rs.Fields(i).Name columnx.Width = ListView1.Width / rs.Fields.Count Next i Do Until rs.EOF Set listx = ListView1.ListItems.Add listx.Text = rs.Fields(0).Value For i = 1 To rs.Fields.Count - 1 Set listsubx = listx.ListSubItems.Add listsubx.Text = rs.Fields(i).Value Next i rs.MoveNext Loop rs.Close Set rs = Nothing cnn.Close Set cnn = Nothing End Sub[local]1[/local][local]2[/local]各位大侠,如图所示的,我运行了之后问题,access数据库也建立了,按了添加按钮,就是不出现要添加的名称,是没有添加到数据库里面的问题吗?还是数据库没保存到?或者是数据库没有跟listview链接好?菜鸟求救。各位救命啊。