VB.NET用代码连接access数据库不成功。方式oledb
我在vs2010中编写vb代码,企图在formload过程中,在datagridview中显示access数据库代码如下:PrivateSubNutrition_Demo...
我在vs2010中编写vb代码,企图在formload过程中,在datagridview中显示access数据库
代码如下:
Private Sub Nutrition_Demo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strconn As String strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Sourse=F:\Nutrition\Nutritions.mdb;jet oledb:database password=123456"
Dim objconn As New OleDb.OleDbConnection(strconn) objconn.Open()
Dim strsql = "select * from DBDB" Dim objadap As New OleDbDataAdapter(strsql, objconn)
objconn.Close()
Dim objDSet As New DataSet()
objadap.Fill(objDSet, "DBDB") DataGridView1.DataSource = objDSet.Tables("DBDB")
End Sub
执行后还是没有数据显示,想请高手具体解答。万分感谢 展开
代码如下:
Private Sub Nutrition_Demo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strconn As String strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Sourse=F:\Nutrition\Nutritions.mdb;jet oledb:database password=123456"
Dim objconn As New OleDb.OleDbConnection(strconn) objconn.Open()
Dim strsql = "select * from DBDB" Dim objadap As New OleDbDataAdapter(strsql, objconn)
objconn.Close()
Dim objDSet As New DataSet()
objadap.Fill(objDSet, "DBDB") DataGridView1.DataSource = objDSet.Tables("DBDB")
End Sub
执行后还是没有数据显示,想请高手具体解答。万分感谢 展开
4个回答
展开全部
去掉objconn.Open()和 objconn.Close(),因为OleDbDataAdapter的Fill方法会自动开启连接和关闭连接
没有没有数据看是否 DBDB表中本来就没有数据?可以用DataTable验证
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strconn As String
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Nutrition\Nutritions.mdb;Jet OLEDB:Database Password=123456"
Dim objconn As New OleDb.OleDbConnection(strconn)
Dim strsql = "select * from DBDB"
Dim objadap As New OleDb.OleDbDataAdapter(strsql, objconn)
Dim tb As New DataTable
objadap.Fill(tb)
DataGridView1.DataSource = tb
End Sub
更多追问追答
追问
我按照你的原封不动输进去,还是没有用。。。我不知道是不是其他地方出问题了
追答
刚才输错了Data Sourse,应该是Data Source,被你的代码迷惑了
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strconn As String
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Nutrition\Nutritions.mdb;Jet OLEDB:Database Password=123456"
Dim objconn As New OleDb.OleDbConnection(strconn)
Dim strsql = "select * from DBDB"
Dim objadap As New OleDb.OleDbDataAdapter(strsql, objconn)
Dim tb As New DataTable
objadap.Fill(tb)
DataGridView1.DataSource = tb
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你选用ACCESS版本是多少呢?
2007以上的Provider=Microsoft.Jet.OLEDB.4.0;Data Sourse=F:\Nutrition\Nutritions.mdb;jet oledb:database password=123456需要改成Provider=Microsoft.ACE.OLEDB.12.0;Data Sourse=F:\Nutrition\Nutritions.mdb;jet oledb:database password=123456
我有应用好的代码,供参考
Try
RecordsetBooks.Close()
ConnectBooks.Close()
Catch
Try
ConnectBooks.Close()
RecordsetBooks.Close()
Catch ex As Exception
End Try
End Try
ConnectBooks.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & NowDatabase & ";Persist Security Info=False")
RecordsetBooks.CursorLocation = ADODB.CursorLocationEnum.adUseClient
RecordsetBooks.Open("Select * From [图书信息]", ConnectBooks, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
DataBooks.DataSource = RecordsetBooks.DataSource
DataBooks.Update()
2007以上的Provider=Microsoft.Jet.OLEDB.4.0;Data Sourse=F:\Nutrition\Nutritions.mdb;jet oledb:database password=123456需要改成Provider=Microsoft.ACE.OLEDB.12.0;Data Sourse=F:\Nutrition\Nutritions.mdb;jet oledb:database password=123456
我有应用好的代码,供参考
Try
RecordsetBooks.Close()
ConnectBooks.Close()
Catch
Try
ConnectBooks.Close()
RecordsetBooks.Close()
Catch ex As Exception
End Try
End Try
ConnectBooks.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & NowDatabase & ";Persist Security Info=False")
RecordsetBooks.CursorLocation = ADODB.CursorLocationEnum.adUseClient
RecordsetBooks.Open("Select * From [图书信息]", ConnectBooks, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
DataBooks.DataSource = RecordsetBooks.DataSource
DataBooks.Update()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的WINDOWS系统是64位的吗,要是64位的,这个程序要改编译环境为x86,否则不行的。因为oledb只是32位的, 64位系统就不能用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
objDSet.Tables(0)
更多追问追答
追问
请问这是什么意思啊。为什么
追答
试下这样,应该可以了!
Dim objDSet As New DataSet()
objadap.Fill(objDSet, "DBDB")
objconn.Close()
DataGridView1.DataSource = objDSet.Tables("DBDB")
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询