我有一个基本的VB.net程序,它针对数据库运行查询以提取客户信息以发送优惠券。我想做的是,当我按下命令按钮返回结果并在运行程序的计算机上创建.csv文件时...现在,我有一个运行查询的程序,并转储报告查看器的结果,但我无法导出到.csv,并且报表查看器上显示的记录太多...
到目前为止,这是我的代码......我假设我需要以XML格式构建布局?
Dim str As New StringBuilder
For Each dr As DataRow In Me.DataDeliveryServiceDataSet.Invoice_Tb
For Each field As Object In dr.ItemArray
str.Append(field.ToString & ",")
Next
str.Replace(",", vbNewLine, str.Length - 1, 1)
Next
Try
My.Computer.FileSystem.WriteAllText("C:\temp\testcsv.csv", str.ToString, False)
Catch ex As Exception
MessageBox.Show("Write Error")
End Try
End Sub
答案 0 :(得分:1)
您可以使用LinQ
替换此短版本的内部循环Dim str As New StringBuilder
For Each dr As DataRow In Me.DataDeliveryServiceDataSet.Invoice_Tb
str.AppendLine(string.Join(",", _
dr.ItemArray.Select(Function(o) if(o = DBNull.Value,"",o.ToString()))))
Next
当然,如果你需要以XML格式导出就足以使用数据表的WriteXml方法了
Me.DataDeliveryServiceDataSet.Invoice_Tb.WriteXml("C:\temp\testcsv.csv")