在vb6.0里面用adodc控件跟datagrid能连接数据库,在VB。net里面怎么连...

 我来答
白吃白喝小乞丐
2012-01-30 · 超过18用户采纳过TA的回答
知道答主
回答量:54
采纳率:0%
帮助的人:47.5万
展开全部
不知道你用什么数据库?给个access的例子(窗体里1个按钮,1个datagridview控件,1个OpenFileDialog控件):

Public mdbsource As String
Public myConnStr As String
Dim myDataset As New DataSet
Dim myBindingSource As New BindingSource

Private Function CreatDataAdapter(ByVal myConn As OleDbConnection, ByVal mytablename As String, ByVal Connstr As String) As OleDbDataAdapter

Dim SQLstr As String = ""
Dim adapter As New OleDbDataAdapter
Dim mySelectCommand As OleDbCommand
SQLstr = "select * from " & mytablename
adapter.SelectCommand = New OleDbCommand(SQLstr, myConn)
Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)
Return adapter
End Function

Private Sub Build_DGV(ByVal mytablename, ByVal ConnStr)
myDataset.Reset()
Dim myConn As New OleDbConnection(ConnStr)
CreatDataAdapter(myConn, mytablename, ConnStr).Fill(myDataset)
myBindingSource.DataSource = myDataset.Tables(0)
DataGridView1.DataSource = myBindingSource
myConn.Close()
myConn.Dispose()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFilePath As String
OpenFileDialog1.Filter = ("Office access文件(*.mdb)|*.mdb|所有文件(*.*)|*.*")
If OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then Exit Sub
If OpenFileDialog1.FileName = "" Then Exit Sub
strFilePath = OpenFileDialog1.FileName
mdbsource = strFilePath
mytablename = "要打开的表名"
myConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbsource & ";Persist Security Info=true"
Build_DGV(mytablename,myConnStr)
End Sub
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式