我想将sql表中的数据写入文本文件。
Dim Itemlist As New SortedList
sql= select SONO,CustomerName,CustomerCode,ItemCode from tbl_SalesOrder where SONO =sonumber
commSQL = New SqlCommand(sql, cnnSQL)
drSQL = commSQL.ExecuteReader
While drSQL.Read
Dim values(drSQL.FieldCount - 1) As Object
Dim fieldCount As Integer = drSQL.GetValues(values)
Itemlist.Add(CStr(drSQL(0)) & "_" & CStr(drSQL(3)), values)
End While
然后我将这些数据写入文本文件。
sr = New StreamWriter(AppPath & "\Import\SalesOrder.txt")
For j = 0 To Itemlist.Count - 1
Sono = CStr(DirectCast(Itemlist.GetByIndex(j), Object())(0))
customername = CStr(DirectCast(Itemlist.GetByIndex(j), Object())(1))
customercode = CStr(DirectCast(Itemlist.GetByIndex(j), Object())(2))
Sitemcode = CStr(DirectCast(Itemlist.GetByIndex(j), Object())(3))
sql = Trim(Sono) & "|" & Trim(customername) & "|" & Trim(customercode) & "|" & Trim(Sitemcode)
sr.WriteLine(sql)
Next j
sr.Close()
Itemlist.Clear()
drSQL.Close()
但这是在排序后将数据写入文本文件。我的情况drsql(3)是项目代码,这就像。' 12'' 25',&# 39; 31',' 45' .so项目列表对此值进行排序,并写入此文件。
我不想像这样排序。我想写入文本文件,就像在表中的数据一样..我可以这样做。
任何帮助都非常明显。
答案 0 :(得分:0)
您正在使用SortedList
,您可以使用ArrayList
,或者使用此类代码可以使用更多可重用且更漂亮的内容:
Dim connection = "your connection string"
Dim command = "your command"
Dim table As New System.Data.DataTable
Dim adapter As New System.Data.SqlClient.SqlDataAdapter(command, connection)
adapter.Fill(table)
Dim builder As New System.Text.StringBuilder
table.Rows.Cast(Of DataRow).ToList() _
.ForEach(Sub(row)
builder.AppendLine(String.Join("|", _
row.ItemArray.Select(Function(value) String.Format("{0}", value).Trim())))
End Sub)
System.IO.File.WriteAllText("your file path", builder.ToString())