使用 vb.net 从数据集/数据表更新 mysql
我是 vb.net 的新手,我正在尝试使用 vb.net 从 txt 文件更新 mysql 表。到目前为止,我已经在这里和那里找到了代码,并且能够从 txt 文件中提取数据,现在我的问题是如何从相同的数据集或 xml 文件更新 mysql。这是我填充 datagrid/dataset/xml 文件的代码。假设“Orden”是 mysql 中的主键,你能告诉我更新 mysql 的最简单方法是什么吗?
OpenFileDialog1.Filter = "Text File|*.txt"
OpenFileDialog1.Title = "Open File..."
OpenFileDialog1.FileName = "trackings"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim DT As New DataTable
DT.Columns.Add("COD")
DT.Columns.Add("Tracking")
DT.Columns.Add("Fecha")
DT.Columns.Add("Orden")
DT.Columns.Add("Estatus")
Dim Lines() As String = System.IO.File.ReadAllLines(OpenFileDialog1.FileName)
For Each Line As String In Lines
Dim ItemsOf() As String = Split(Line, " ")
ItemsOf = Line.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
If ItemsOf(0) = "N" Then ItemsOf(4) = 3 Else ItemsOf(4) = 6
Dim NRow As String() = {ItemsOf(0), ItemsOf(1), ItemsOf(2), ItemsOf(3), ItemsOf(4)}
DT.Rows.Add(NRow)
Next Line
DataGridView1.DataSource = DT
Dim ds As New DataSet
ds.Tables.Add(DT)
ds.WriteXml("c:\x.xml")
End If
任何帮助表示赞赏!还有一些代码甚至更多! =0)
I am new to vb.net and I am trying to update mysql table from a txt file using vb.net. So far I've found code here and there and been able to extract the data from the txt file, now my question is how to update mysql from the same dataset or xml file. Here is my code to populate the datagrid/dataset/xml file. Can you tell me what is the easiest way to update mysql assuming "Orden" is my primary key in mysql.
OpenFileDialog1.Filter = "Text File|*.txt"
OpenFileDialog1.Title = "Open File..."
OpenFileDialog1.FileName = "trackings"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim DT As New DataTable
DT.Columns.Add("COD")
DT.Columns.Add("Tracking")
DT.Columns.Add("Fecha")
DT.Columns.Add("Orden")
DT.Columns.Add("Estatus")
Dim Lines() As String = System.IO.File.ReadAllLines(OpenFileDialog1.FileName)
For Each Line As String In Lines
Dim ItemsOf() As String = Split(Line, " ")
ItemsOf = Line.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
If ItemsOf(0) = "N" Then ItemsOf(4) = 3 Else ItemsOf(4) = 6
Dim NRow As String() = {ItemsOf(0), ItemsOf(1), ItemsOf(2), ItemsOf(3), ItemsOf(4)}
DT.Rows.Add(NRow)
Next Line
DataGridView1.DataSource = DT
Dim ds As New DataSet
ds.Tables.Add(DT)
ds.WriteXml("c:\x.xml")
End If
Any help is appreciated! And with some code even more! =0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的代码,您的平面文件似乎没有以复杂的方式格式化。如果是这种情况,您可以完全跳过生成 XML 文件。使用“ 将文件直接加载到 MySQL加载数据内文件”。
如有必要,请先加载到临时表,然后使用该表执行更新。
Based on your code, it looks like your flat file isn't formatted in a complicated manner. If this is the case, you can skip generating the XML file altogether. Load the file directly onto MySQL using "LOAD DATA INFILE".
If necessary, load onto a staging table first, and then perform the update using that table.