2024-07-04 03:00:06
见下面的代码,自己换成真实的数据库及表即可
Imports System.Data
Imports System.IO
Imports System.Data.OleDb
Module Module1
Public cn1 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ACCESS数据库1.mdb") '定义连接1---这里请更改为实际数据库路径及名称
Public cn2 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ACCESS数据库2.mdb") '定义连接2---这里请更改为实际数据库路径及名称
Public DataBaseRST1 As Integer '用来返回数据库执行结果
Public DataBaseRST2 As Integer '用来返回数据库执行结果
Public Function DataModify(ByVal str_cmd1 As String, ByVal str_cmd2 As String) As Boolean '进行数据库修改操作函数
Dim cmdinsert1 As New OleDbCommand
Dim cmdinsert2 As New OleDbCommand
Try
cmdinsert1.CommandText = str_cmd1
cmdinsert2.CommandText = str_cmd2
cmdinsert1.Connection = cn1
cmdinsert2.Connection = cn2
If cn1.State = ConnectionState.Closed Then cn1.Open()
If cn2.State = ConnectionState.Closed Then cn2.Open()
DataBaseRST1 = cmdinsert1.ExecuteNonQuery() '用来返回执行的结果
DataBaseRST2 = cmdinsert2.ExecuteNonQuery() '用来返回执行的结果
cn1.Close()
cn2.Close()
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
End Module
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataModify("insert into aa values ('1','2')", "insert into aa values ('1','2')") '调用方法
End Sub
End Class
这个方法我只能用于添加修改删除,请问,查询数据并且绑定到DGV怎么做?
Public Function Search(ByVal cn1 As OleDb.OleDbConnection, ByVal cn2 As OleDb.OleDbConnection, ByVal str_cmd1 As String, ByVal str_cmd2 As String, ByVal DGV1 As DataGridView, ByVal DGV2 As DataGridView) As Boolean '查询 str_cmd1,str_cmd2---查询命令,DGV1,DGV2---DataGridView,用来显示数据的控件
Dim tb1 As New DataTable
Dim tb2 As New DataTable
Try
Dim ap1 As New OleDb.OleDbDataAdapter(str_cmd1, cn1)
ap1.Fill(tb1)
DGV1.DataSource = tb1
Dim ap2 As New OleDb.OleDbDataAdapter(str_cmd2, cn2)
ap2.Fill(tb2)
DGV2.DataSource = tb2
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
你好,我是要合并数据放在一个DGV里面的。
Public Function Search(ByVal cn1 As OleDb.OleDbConnection, ByVal cn2 As OleDb.OleDbConnection, ByVal str_cmd1 As String, ByVal str_cmd2 As String, ByVal DGV As DataGridView) As Boolean '查询 str_cmd1,str_cmd2---查询命令,DGV---DataGridView,用来显示数据的控件
Dim tb1 As New DataTable
Dim tb2 As New DataTable
Try
Dim ap1 As New OleDb.OleDbDataAdapter(str_cmd1, cn1)
ap1.Fill(tb1)
Dim ap2 As New OleDb.OleDbDataAdapter(str_cmd2, cn2)
ap2.Fill(tb2)
For i = 0 To tb2.Rows.Count - 1
tb1.Rows.Add(tb2.Rows(i))
Next
DGV.DataSource = tb1
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Function Search(ByVal cn1 As OleDb.OleDbConnection, ByVal cn2 As OleDb.OleDbConnection, ByVal str_cmd1 As String, ByVal str_cmd2 As String, ByVal DGV As DataGridView) As Boolean '查询 str_cmd1,str_cmd2---查询命令,DGV1,DGV2---DataGridView,用来显示数据的控件
Dim tb1 As New DataTable
Dim tb2 As New DataTable
Try
Dim ap1 As New OleDb.OleDbDataAdapter(str_cmd1, cn1)
ap1.Fill(tb1)
Dim ap2 As New OleDb.OleDbDataAdapter(str_cmd2, cn2)
ap2.Fill(tb2)
For i = 0 To tb2.Rows.Count - 1
tb1.Rows.Add(tb2.Rows(i).ItemArray)'这里变了
Next
DGV.DataSource = tb1
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
分肯定是你的了,但我真心是想跟你交朋友。与智者同行。
我是搞自动化开发的,vb.net是自学的,皮毛而已
我是个还没毕业的大学生,现在在公司实习。如不嫌弃,交个朋友吧
只要你申请,都是会添加的
2024-07-04 01:08:57
两个库同时操作,我想不通,我是新手,给点代码和建议好吗?
不好意思, 我已经绕道了, 不用送我~~
不送