使用值XML元素作为过程的变量

时间:2013-12-12 18:28:58

标签: sql xml vb.net

我不熟悉在VB中使用XML,我尝试使用Element DBName的值来查找要连接的活动SQL数据库名称。我查看了很多代码,但它让人更加困惑。任何帮助都会很棒!

下面是我试图使用变量的子代码。我试图从xml文档中获取DBName。 xml的结构如下

<db>
<User>DBUser2</User>
<Password><![CDATA[xka2bOHaQZWesxHLFHlWaVw7JscbNFCobXbqYWc5rwppoNkAn3K1uqriSCHdEzyY/FNDdbgRJTzDEIM8Jc5PYTBzfMUC5UIDtr16a64Xj7MRGI4/AvRcys/fIQDZQ947GesAc1rF/kbZu8AaZDVTjwObbNPT2L/h+IA6WjM9lqv6BOCi4dUeKxx5AneCBy2TJdifxEPdAIOT9lqTm5/aHFD0JgqSn0OTtWbLuYX9KX9uvA8L8zEH51yEmGl258aRVfpGfyxph/cpdnW1miRk4Q==]]></Password>
<Server>N127.0.0.1\CESSQL</Server>
<ServerDatabase>Marino</ServerDatabase>
<BackupServer></BackupServer>
<BackupDatabase></BackupDatabase>
<MasterPassword><![CDATA[EFmUxlkmQfIx4w18oQZ1dtCxAIXIyBZPCelL8csYX3E5NuHBZNI42UXNhFxmu87Ksj5CbQpC1WNTj4jnLaaq7nX6Oa4z3M7glLAeRaXWGAd3VqWfADRQAW3RCKKSJRMK3jwRWHJjY1Vp2hgn9CuMACvYHZUrUyK6nJ9HMiwaXcUJKtm4vl0toQNpwIuGvT2cfMJgvpjXJhTBfxKE75ZWeAldXhX5h/c6LYMQ6DE79uuhdbisfmIrXTskKTcceiRjWU2jTFumpjhM1tUqEoBFLw==]]></MasterPassword>

Imports System.Data
Imports System.IO
Imports System.Xml
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        m_xmld = New XmlDocument()
        m_xmld.Load("C:\FPOS5\Data\dbinfo.xml")
        m_nodelist = m_xmld.SelectNodes("/DB/ServerDatabase")
        For Each m_node In m_nodelist
        Dim ServerDatabaseValue =    m_node.Attributes.GetNamedItem("ServerDatabase").Value
        Console.Write(ServerDatabaseValue)
        Next

    Dim con As New SqlClient.SqlConnection("server=127.0.0.1\CESSQL;database=" & DBName & ";User ID=;Password=")
    Dim cmd As New SqlClient.SqlCommand
    Dim DriverNumber2 As Integer
    Dim CheckNumber2 As Integer

    DriverNumber2 = TextBox1.Text
    CheckNumber2 = TextBox2.Text

    Try
        con.Open()
    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try
    cmd = New SqlClient.SqlCommand("Update Sale Set DriverID =" & DriverNumber2 & "where CheckNumber =" & CheckNumber2 & "")


    cmd.Connection = con
    cmd.ExecuteNonQuery()
    con.Close()

    MsgBox("Done")
End Sub

2 个答案:

答案 0 :(得分:0)

我不确定您想要什么,但您需要解析XML并使用连接字符串中的XML节点/元素。我不确定这是一个好主意,看到这个链接在VB中解析XML很容易。http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET

答案 1 :(得分:0)

如果您的问题是关于如何从XML文件中读取,请举例说明。

假设已声明:

Dim xml = <db>
            <User>DBUser2</User>
            <Password><![CDATA[xka2bOHaQZWesxHLFHlWaVw7JscbNFCobXbqYWc5rwppoNkAn3K1uqriSCHdEzyY/FNDdbgRJTzDEIM8Jc5PYTBzfMUC5UIDtr16a64Xj7MRGI4/AvRcys/fIQDZQ947GesAc1rF/kbZu8AaZDVTjwObbNPT2L/h+IA6WjM9lqv6BOCi4dUeKxx5AneCBy2TJdifxEPdAIOT9lqTm5/aHFD0JgqSn0OTtWbLuYX9KX9uvA8L8zEH51yEmGl258aRVfpGfyxph/cpdnW1miRk4Q==]]></Password>
            <Server>N127.0.0.1\CESSQL</Server>
            <ServerDatabase>Marino</ServerDatabase>
            <BackupServer></BackupServer>
            <BackupDatabase></BackupDatabase>
            <MasterPassword><![CDATA[EFmUxlkmQfIx4w18oQZ1dtCxAIXIyBZPCelL8csYX3E5NuHBZNI42UXNhFxmu87Ksj5CbQpC1WNTj4jnLaaq7nX6Oa4z3M7glLAeRaXWGAd3VqWfADRQAW3RCKKSJRMK3jwRWHJjY1Vp2hgn9CuMACvYHZUrUyK6nJ9HMiwaXcUJKtm4vl0toQNpwIuGvT2cfMJgvpjXJhTBfxKE75ZWeAldXhX5h/c6LYMQ6DE79uuhdbisfmIrXTskKTcceiRjWU2jTFumpjhM1tUqEoBFLw==]]></MasterPassword>
          </db>

这只是一行代码:

xml.Element("ServerDatabase").Value

或者,保留变量名称:

Dim ServerDatabaseValue As String = xml.Element("ServerDatabase").Value

始终指定变量类型。为了帮助您,您可以在项目设置中设置Option Strict OnOption Infer Off。这可以通过强迫您进入某些(良好的)开发习惯来提高代码质量。