首先选择需要的记录到数据集中
然后使用以下函数将数据集转换为 CSV 文件。
将数据集转换为 CSV 的函数
Sub SetDataTable_To_CSV(ByVal dtable As DataTable, ByVal path_filename As String, ByVal sep_char As String)
Dim writer As System.IO.StreamWriter
Try
writer = New System.IO.StreamWriter(path_filename)
Dim _sep As String = ""
Dim builder As New System.Text.StringBuilder
For Each col As DataColumn In dtable.Columns
builder.Append(_sep).Append(col.ColumnName)
_sep = sep_char
Next
writer.WriteLine(builder.ToString())
For Each row As DataRow In dtable.Rows
_sep = ""
builder = New System.Text.StringBuilder
For Each col As DataColumn In dtable.Columns
builder.Append(_sep).Append(row(col.ColumnName))
_sep = sep_char
Next
writer.WriteLine(builder.ToString())
Next
Catch ex As Exception
Finally
If Not writer Is Nothing Then writer.Close()
End Try
End Sub
取自以下链接
http://redsouljaz.com/2010/06/03/vb-net-set-data-table-to-csv-file/
如果你想通过 VB.net 处理我的 sql 端。
然后你可以创建存储过程
CREATE PROC ConvertCSV
AS
BEGIN
SELECT SUBSTRING(
(SELECT ' ' + s.BPl + ',' + s.Lo + ',' + s.Ltion + ',' + s.Ite + ',' + s.HNO + ',' + s.QNo + ',' + s.Country + ',' + s.STATE + ',' + s.Rem + CHAR(10)
FROM accountexample s
ORDER BY s.BPl
FOR XML PATH('')),2,200000) AS CSV
END
然后从 vb.net 调用这个过程
Dim con As New SqlConnection("Data Source=<datasource>;Initial Catalog=<databasename>;User ID=<username>;Password=<password>")
con.Open()
Dim cmd As New SqlCommand("ConvertCSV", con)
Try
cmd.CommandType = CommandType.StoredProcedure
cmd.ExecuteNonQuery()
Finally
If cmd IsNot Nothing Then cmd.Dispose()
If cn IsNot Nothing AndAlso cn.State <> ConnectionState.Closed Then cn.Close()
End Try
希望这可以帮助