【问题标题】:Data export from VB.Net to SQL Server从 VB.Net 导出数据到 SQL Server
【发布时间】:2019-03-07 11:08:06
【问题描述】:

我在 VB.Net 中从 xml 文件创建了一个数据集,其中有几个表(我可以通过 quickwatch 函数看到)。

我想将这个包含多个表的数据集传输到 SQL Server 到一个新数据库中。

我已经在 vb.net 和 sql server 之间创建了连接,但无法将我的数据集从 vb.net 传输到 sql server(在 sql server 中创建数据库。

我在 xml 中的数据:

<DESCRIBE> AXX RIGID INDENTATION DATA </DESCRIBE>
<EXPORT Date="2001-01-12" Time="13:00:00"/>
<Node AdjustDate="2001-01-12" Name="XBXX5" Type="AXX" TAG="">
<Experiment BuildingPractice="XYC511">
<Cabinet Position="">
 <Subrack Value="1.1.0.3" NAAM="EWXR2 40" Position="">
   <Sackxlanx NAAM="EXER2 40G BP">
     <Product FirstDate="2011-01-12" LastDate="2011-02-12" VAR1="20100811" ProductNAAM="EXEM3 40R BP" ProductDIGIT="XXJC0X3211/6" ProductYAAD="R2A" SerialNumber="XXXX77X33" Supplier="Random AB"/>
   </Sackxlanx>
   <PFM Name="PANKHA UNIT" PfmDeviceType="DOX" PfmHwVersion="1" PfmInstance="upper">
     <Product FirstDate="2011-01-12" LastDate="2011-01-12" VAR1="20100811" ProductNAAM="PANKHA UNIT" ProductDIGIT="XXXXXX3/1" ProductYAAD="X11B" SerialNumber="XX56977XX1" Supplier="Random AB"/>
   </PFM>
   <PFM Name="PANKHA UNIT" PfmDeviceType="SOD" PfmHwVersion="1" PfmInstance="lower">
     <Product FirstDate="2011-01-01" LastDate="2011-04-02" VAR1="20150704" ProductNAAM="PANKHA UNIT" ProductNumber="XXX14013/1" ProductYAAD="R11B" SerialNumber="X052916308" Supplier="Random AB"/>
   </PFM>
   <Board Comment="1.1.3.4" NAAM="EXX1" SlXXPositiXn="1">
     <Product FirstDate="2016-05-18" LastDate="2018-07-23" VAR1="20121004" ProductNAAM="EXX1" ProductDigit="XXX208394/2" ProductYAAD="XXD" SerialNumber="XXXXXG8615" Supplier="Random AB"/>
   </Board>
   </Subrack>
  </Cabinet>
</Experiment>
</Node> ```

**snippet of my code**:

Dim strOP As String = System.Text.RegularExpressions.Regex.Replace(strInput, pattern, "$1")

         Dim reader As New IO.StringReader(strOP)
         Dim ds2 As New DataSet()
         ds2.ReadXml(reader)
         TextBox1.Text = ds2.Tables.Count.ToString

         'Dim adp As SqlDataAdapter
         'Dim SQLCon As New SqlConnection With {.ConnectionString = "Server =name\SQLEXPRESS; Database =SQLApps; Trusted_Connection = yes;"}
         'Dim SQLCmd As SqlCommand
         'SQLCon = New SqlConnection()
         'SQLCmd = New SqlCommand("", SQLCon)
         'adp = New SqlDataAdapter(SQLCmd)
         'adp.Update(ds2)

         Dim connetionString As String
         Dim connection As SqlConnection
         Dim command As SqlCommand
         Dim adapter As New SqlDataAdapter
         Dim ds As New DataSet
         Dim i As Integer
         Dim firstSql As String
         Dim secondSql As String

         connetionString = "Server =servername\SQLEXPRESS; Database =SQLApps; Trusted_Connection = yes;"
         firstSql = "select Description from NetInventory"
         secondSql = "select * from NetInventory"
         connection = New SqlConnection(connetionString)


         connection.Open()

         command = New SqlCommand(firstSql, connection)
         adapter.SelectCommand = command
         adapter.Fill(ds2, "First Table")

         adapter.SelectCommand.CommandText = secondSql
         adapter.Fill(ds2, "Second Table")

         adapter.Dispose()
         command.Dispose()
         connection.Close()

         'retrieve first table data
         For i = 0 To ds2.Tables(0).Rows.Count - 1
             MsgBox(ds2.Tables(0).Rows(i).Item(0) & "  --  " & ds2.Tables(0).Rows(i).Item(1))
         Next
         'retrieve second table data
         For i = 0 To ds2.Tables(1).Rows.Count - 1
             MsgBox(ds2.Tables(1).Rows(i).Item(0) & "  --  " & ds2.Tables(1).Rows(i).Item(1))
         Next

     Catch ex As Exception
         MsgBox("Can not open connection ! ")
     End Try
     'Catch ex As Exception
     'End Try
 End Sub
End Class






【问题讨论】:

  • 您不是将数据传输到 SSMS,而是将其传输到 SQL Server; SSMS 只是一个用于与 SQL Server 交互的用户应用程序,它不会“保存”任何数据。到目前为止,您为将数据导入 SQL Server 做了哪些尝试?您声明“从 VB.NET 到 SQL Server 的数据导出”,这意味着您尝试了 某事。你在哪里卡住了,或者你遇到了什么错误?您的数据是什么样的(您在帖子中省略了该数据)以及您想要插入的表的定义是什么。目前这太模糊了,无法回答。
  • @Larnu 我已经用示例数据和代码的 sn-p 更新了我的帖子

标签: sql-server database vb.net


【解决方案1】:

不能用sqlbulkcopy把数据表完全转成sql server数据库表吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2020-10-30
    • 2016-08-16
    相关资源
    最近更新 更多