当前位置:编程学习 > VB >>

急!!!!!!多数据库连接问题

有几个数据库:
控件有combo控件、日期控件、5个文本框控件。一个添加按钮
实现的功能:通过一个combo控件,里面的值是数据库名,当点击combo控件选择数据库名时,就连接哪个数据库,然后显示在datagrid控件中,有一个添加按钮,当单击添加时则将文本框的内容添加到相对应的数据库中,并显示在datagrid控件中。但是我点击添加按钮时,不能添加到数据库中。
部分代码:
combo单击事件
Private Sub datasource_Click()
Select Case datasource.ListIndex
Case 0
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 生化 "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 1
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 免疫(械) "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 2
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 免疫(药) "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 3
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 澳斯邦 "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 4
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 仪器设备 "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 5
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 耗材 "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 End Select
End Sub

添加事件
Private Sub add_Click()

If Adodc1.Recordset.BOF = False And Adodc1.Recordset.EOF = False Then
Adodc1.Recordset("产品名称") = txtname.Text
Adodc1.Recordset("注册证号") = txtnumber.Text
Adodc1.Recordset("生产单位") = home1.Text
Adodc1.Recordset("提供单位") = home2.Text
Adodc1.Recordset("受理号") = neednumber.Text
Adodc1.Recordset("有效期") = time1.Value
MsgBox "添加成功"
Else
txtname.Text = ""
txtnumber.Text = ""
home1.Text = ""
neednumber.Text = ""
home2.Text = ""
End If
End Sub

是不是哪里没完善的呀。
--------------------编程问答--------------------

Private Sub add_Click() 

If Adodc1.Recordset.BOF = False And Adodc1.Recordset.EOF = False Then 
Adodc1.Recordset.AddNew
Adodc1.Recordset("产品名称") = txtname.Text 
Adodc1.Recordset("注册证号") = txtnumber.Text 
Adodc1.Recordset("生产单位") = home1.Text 
Adodc1.Recordset("提供单位") = home2.Text 
Adodc1.Recordset("受理号") = neednumber.Text 
Adodc1.Recordset("有效期") = time1.Value 
Adodc1.Recordset.Update
Adodc1.Refresh
MsgBox "添加成功" 
Else 
txtname.Text = "" 
txtnumber.Text = "" 
home1.Text = "" 
neednumber.Text = "" 
home2.Text = "" 
End If 
End Sub 

--------------------编程问答-------------------- Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False" 

Adodc1.Refresh: Set DataGrid1.datasource = Adodc1 
题外话,这两条都是一样的,用得着都放select case 里面嚒... --------------------编程问答--------------------
引用 2 楼 king06 的回复:
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False"

Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
题外话,这两条都是一样的,用得着都放select case 里面嚒...

恩,是不需要的 --------------------编程问答-------------------- 还是不可以哦,datagrid能和数据库连接起来,但是单击添加按钮时还是没有用,也不报错,就是没有数据存入到相对应的数据库中。 --------------------编程问答-------------------- 看看你的Adodc1属性,设置
CursorLocation =3
CursorType=1
LockType=3
--------------------编程问答-------------------- 添加事件
Private Sub add_Click()
Select Case datasource.ListIndex
Case 0
Adodc1.RecordSource = "select * from 生化 "
If Adodc1.Recordset.BOF = False And Adodc1.Recordset.EOF = False Then
Adodc1.Recordset.AddNew
Adodc1.Recordset("产品名称") = Trim(txtname.Text)
Adodc1.Recordset("有效期") = time1.Value
Adodc1.Recordset("注册证号") = Trim(txtnumber.Text)
Adodc1.Recordset("生产单位") = Trim(home1.Text)
Adodc1.Recordset("提供单位") = Trim(home2.Text)
Adodc1.Recordset("受理号") = Trim(neednumber.Text)
Adodc1.Recordset.Update
Adodc1.Refresh
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1

Else
txtname.Text = ""
home1.Text = ""
home2.Text = ""
'time1.Value = ""
txtnumber.Text = ""
neednumber.Text = ""
End If
End Select
End Sub
combo控件单击事件
Private Sub datasource_Click()
Select Case datasource.ListIndex
Case 0
Adodc1.RecordSource = "select * from 生化 "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 1
Adodc1.RecordSource = "select * from 免疫(械) "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 2
Adodc1.RecordSource = "select * from 免疫(药) "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 3
Adodc1.RecordSource = "select * from 澳斯邦 "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 4
Adodc1.RecordSource = "select * from 仪器设备 "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 Case 5
Adodc1.RecordSource = "select * from 耗材 "
Adodc1.Refresh: Set DataGrid1.datasource = Adodc1
 End Select
End Sub
为什么单击添加按钮,数据没有添加到数据库中,但是也不报错,是哪里出了问题。有谁能解决吗?很急哦 --------------------编程问答-------------------- Adodc1.Refresh: Set DataGrid1.datasource = Adodc1 这句去掉,
设计时在DataGrid1的属性里选中Adodc1就是了
--------------------编程问答-------------------- 上面所有的Set DataGrid1.datasource = Adodc1 都可以去掉,Adodc1.Refresh保留 --------------------编程问答-------------------- 不行,试了,不是这个问题,点击combo里的数据库名时,datagrid控件是可以显示数据库中的字段的,只是点击添加按钮时不能再datagrid中显示相应的内容。 --------------------编程问答-------------------- Private Sub datasource_Click()
   Dim ArrStr
   ArrStr = Array("生化", "免疫(械)", "免疫(药)", "澳斯邦", "仪器设备", "耗材")
   Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False"
   Adodc1.RecordSource = "select * from " & ArrStr(DataSource.ListIndex)
   Set DataGrid1.DataSource = Adodc1
   If Adodc1.Recordset.RecordCount > 0 Then Adodc1.Recordset.MoveFirst
End Sub

Private Sub add_Click()
   Adodc1.Recordset.AddNew
   Adodc1.Recordset("产品名称") = txtname.Text
   Adodc1.Recordset("注册证号") = txtnumber.Text
   Adodc1.Recordset("生产单位") = home1.Text
   Adodc1.Recordset("提供单位") = home2.Text
   Adodc1.Recordset("受理号") = neednumber.Text
   Adodc1.Recordset("有效期") = time1.Value
   Adodc1.Recordset.Update
   Adodc1.Refresh
   MsgBox "添加成功"
   txtname.Text = ""
   txtnumber.Text = ""
   home1.Text = ""
   neednumber.Text = ""
   home2.Text = ""
End Sub
--------------------编程问答--------------------
引用 10 楼 cbm666 的回复:
Private Sub datasource_Click()
  Dim ArrStr
  ArrStr = Array("生化", "免疫(械)", "免疫(药)", "澳斯邦", "仪器设备", "耗材")
  Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\company.mdb;Persist Security Info=False"
  Adodc1.RecordSource = "select * from " & ArrStr(DataSource.ListIndex)
  Set DataGrid1.DataSource = Adodc1
  If Adodc1.Recordset.RecordCount > 0 Then Adodc1.Recordset.MoveFirst
End Sub

Private Sub add_Click()
  Adodc1.Recordset.AddNew
  Adodc1.Recordset("产品名称") = txtname.Text
  Adodc1.Recordset("注册证号") = txtnumber.Text
  Adodc1.Recordset("生产单位") = home1.Text
  Adodc1.Recordset("提供单位") = home2.Text
  Adodc1.Recordset("受理号") = neednumber.Text
  Adodc1.Recordset("有效期") = time1.Value
  Adodc1.Recordset.Update
  Adodc1.Refresh
  MsgBox "添加成功"
  txtname.Text = ""
  txtnumber.Text = ""
  home1.Text = ""
  neednumber.Text = ""
  home2.Text = ""
End Sub


能告诉我,我错哪里了,谢谢,请指教一下,我错了哪个地方,谢谢! --------------------编程问答-------------------- 学习! --------------------编程问答-------------------- 不太喜欢用Adodc1直接绑定到数据控件中来。
定义一个ADODB.Connection,ADODB.RecordSet对象来,
实例出不同指向的数据库连接对象,将请求的记录放在RecordSet中,然后加载到显示控件中也未尝不可。
补充:VB ,  数据库(包含打印,安装,报表)
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,