主题:VB2008怎么连接数据库
1375784113
[专家分:0] 发布于 2010-05-05 20:33:00
VB2008 ado连接数据库
'引用ADODB
Dim conn As New ADODB.Connection '定义数据库对象
Dim rs As New ADODB.Recordset '定义记录集对象
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\denglu.mdb;Jet OLEDB:Database Password=123456"
conn.Open()
rs = New ADODB.Recordset
rs.ActiveConnection = conn
rs.Open("Select * From biao1", conn, 1, 3)
最后更新于:2010-05-10 08:55:00
回复列表 (共6个回复)
板凳
1375784113 [专家分:0] 发布于 2010-05-07 18:54:00
[em1]
3 楼
merry05 [专家分:8920] 发布于 2010-05-08 11:51:00
in vs.net development environment,you could get access to Access database by the DB class,such as oledbconnection,oledbdatareader,oledbcommand and so on
vb.net is quite different from vb6,it will comes as a surprise to learn that the key word "return" appears in vb.net
Confronting obstacles,you should consult MSDN which covers all aspects of Visual Studio
4 楼
1375784113 [专家分:0] 发布于 2010-05-09 09:19:00
Imports System.Data.OleDb'引用放在最上面
Public Class xiugaifrm
Private Sub xiugaifrm_Load
Dim Conn As OleDbConnection
Conn = New OleDbConnection
Conn.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\denglu1.mdb;Jet OLEDB:Database Password=123456")
Conn.Open() '打开数据库
If ConnectionState.Open Then '判断数据库目前的状态是否已打开
MsgBox("数据库己打开")
endif
End Sub
End Class
数据库我用ADO.net打开了,不知道用什么方法把数据读取出来,比如读取第一、第二列数据存放在textbox1,textbox2两个文本框里?
5 楼
merry05 [专家分:8920] 发布于 2010-05-09 12:00:00
yeah,you have secceeded in getting access to DB,congratulations
but how to fetch the data you want? u should hava a shot at consulting MSDN
i'll provide some key words:datareader,dataadapter,datatable,dataset
your remarks was shot throught with puzzle,i holp files online could shed some light on your enigma
good luck
6 楼
1375784113 [专家分:0] 发布于 2010-05-09 21:39:00
Private Sub xiugaifrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim connstr As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\denglu1.mdb;Jet OLEDB:Database Password=123456")
Dim conn As New OleDbConnection(connstr)
conn.Open()
' 打开数据库连接()
If ConnectionState.Open Then '判断数据库目前的状态是否已打开
'MsgBox("打开")
Dim sql1 As String = "select * from biao1"
Dim cmd As New OleDbCommand(sql1, conn)
Dim dr1 As OleDbDataReader = cmd.ExecuteReader
While dr1.Read = True
' dr1.NextResult()
TextBox7.Text = dr1.GetString(0)
TextBox1.Text = dr1.GetString(1) '读取A表中第二个字段的内容到文本框。
TextBox2.Text = dr1.GetString(2) '读取A表中第二个字段的内容到文本框。
TextBox3.Text = dr1.GetString(3) '读取A表中第二个字段的内容到文本框。
TextBox4.Text = dr1.GetString(4)
TextBox5.Text = dr1.GetString(5) '读取A表中第二个字段的内容到文本框。
TextBox6.Text = dr1.GetString(6)
dr1.Close() '(关闭数据读取器())
conn.Close() '(关闭数据库连接())
Exit Sub
End While
End If
End Sub
我读取了记录写入文本框,只不过不能对类型为Integer的字段进行读取,还有不知道怎么移动记录指针,请高手指教一下,最好有源码.
我来回复