更新远程访问服务器

时间:2013-12-11 10:43:55

标签: sql vb.net ms-access

在创建我的程序时,我发现使用软件包提供本地数据库会更加可靠,而不是为每个实例连接到远程数据库。

以下是关于如何操作的想法,但是我希望它能够自动发现它找到的每个不同的主机,而不是选择每个成员并按下按钮。这是我不知道如何实现的。

Imports System.Data.OleDb

Public Class Form1    
Dim con as oledbconnection = new oledbconnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='.\localDB.accdb'")
Dim rem as oledbconnection = new oledbconnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='\\server\remote.accdb'")
dim da as new oledbdataadaptor
dim dt as new datatable
dim dt2 as new datatable
Dim Str as string
Dim cmd as new oledbcommand(str, con)

Private Sub Form1_load
con.open()
da.SelectCommand = New OleDbCommand("select distinct Host from Logs")
da.fill(dt)
con.close

Combobox1.datasource = dt
Combobox1.displaymember = "Host"

End Sub

Private Sub button1_click
con.open()
Dim cmd As New OleDbCommand("select * from local where host=@host;", con)
cmd.parameters.addwithvalue("@host", combobox1.text.tostring)
da.fill(dt2)
con.close()

rem.open()
Dim cmd1 As New OleDbCommand("update remote set col1=@col1 where ID=@ID;", con)

cmd1.parameters.addwithvalue("@col1", dt2.rows(0).item(1).tostring)
cmd1.parameters.addwithvalue("@ID", dt2.rows(0).item(0).tostring)
rem.close()

End Sub
End Class

任何想法都会受到赞赏。

*作为一个注释,我意识到JET提供了同步能力,但数据库必须采用2007-10格式,因此ACE是唯一的选择。

1 个答案:

答案 0 :(得分:0)

首先OleDbCommand不接受命名参数,因此您需要使用?引用它们,并按照它们出现的顺序传递参数。

Dim cmd As New OleDbCommand("select * from local where host = ?", con)
cmd.parameters.Add(combobox1.Text.ToString)

完成后,您需要使用SQL句子的结果填充DataSet

    Dim DA As New OleDbDataAdapter(cmd)
    Dim DS As New DataSet
    DA.Fill(DS, "hosts")

<强>更新

为了拥有与你相同的代码,我会做这样的事情

Imports System.Data.OleDb

Public Class Form1

Dim con as oledbconnection = new oledbconnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='.\localDB.accdb'")
Dim rem as oledbconnection = new oledbconnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='\\server\remote.accdb'")

dim dt as New DataTable
Dim Str as String
Dim cmd as New OleDbCommand(str, con)


Private Sub Form1_load

con.open()
da.SelectCommand = New OleDbCommand("select distinct Host from Logs")
da.fill(dt)
con.close


For Each DR as DataRow In DT.Rows

con.open()
Dim cmd As New OleDbCommand("select * from local where host= ? ", con)
cmd.Parameters.Add(DR.Item(0))
Dim DA As New SqlClient.SqlDataAdapter(cmd)
Dim DS As New DataSet
da.Fill(DS,"hosts")
con.close()

rem.open()
Dim cmd As New OleDbCommand("update remote set col1=? where ID=?", rem)

cmd.Parameters.Add(DS.Tables("hosts").Rows(0).Item(1).ToString)
cmd.Parameters.Add(DS.Tables("hosts").Rows(0).Item(0).ToString)
cmd.ExecuteNonQuery()
rem.close()

Next

End Sub
End Class