在vb6.0里面用adodc控件跟datagrid能连接数据库,在VB。net里面怎么连...
1个回答
展开全部
不知道你用什么数据库?给个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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询