所以我有这段代码
Using FileObject As New FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)
Using StreamWriterObj As New StreamWriter(FileObject)
connect.Open()
Using reader As SqlDataReader = command.ExecuteReader()
Dim FieldCount As Integer = reader.FieldCount - 1
Do While reader.Read()
StreamWriterObj.Write(reader.Item(0))
For i = 1 To FieldCount
StreamWriterObj.Write(" @ ")
StreamWriterObj.Write(reader.Item(i))
Next
StreamWriterObj.WriteLine()
Loop
End Using
connect.Close()
End Using
End Using
它基本上将我的SQL查询结果设置为StreamWriter对象并将其导出为.txt文件。 以下是选择查询后的结果示例:
+---------------------------------------+
| Results |
+---------------------------------------+
+ +
| Document No.| # | col 3 | col 4 |
+-------------+-------+--------+--------+
| 333456 | 0 | "value"| "value"|
+-------------+-------+--------+--------+
| 333456 | 0 | "value"| "value"|
+-------------+-------+--------+--------+
| 462345 | 0 | "value"| "value"|
+-------------+-------+--------+--------+
| 585357 | 0 | "value"| "value"|
+-------------+-------+--------+--------+
所以我的任务是以这样的方式制作它,当我导出到.txt文件时,里面会看起来像这样:
+---------------------------------------+
| Results |
+---------------------------------------+
+ +
| Document No.| # | col 3 | col 4 |
+-------------+-------+--------+--------+
| 333456 | 1 | "value"| "value"|
+-------------+-------+--------+--------+
| 333456 | 2 | "value"| "value"|
+-------------+-------+--------+--------+
| 333456 | 3 | "value"| "value"|
+-------------+-------+--------+--------+
| 462345 | 1 | "value"| "value"|
+-------------+-------+--------+--------+
| 585357 | 1 | "value"| "value"|
+-------------+-------+--------+--------+
| 585357 | 2 | "value"| "value"|
+-------------+-------+--------+--------+
或者换句话说,根据文档号来计算(下一个数字)行。 (选择查询按文档编号排序)。
我认为一个明智的解决方案是使用StringBuilder,但我需要一些帮助。
谢谢
答案 0 :(得分:0)
id在实际的sql中执行: -
WITH
q AS
(select query)
,
sequenced as (
SELECT
ROW_NUMBER() OVER (PARTITION BY [Document no. Column] ORDER BY [column name]
) AS sequence_id,
*
FROM
q
)
SELECT
*
FROM
sequenced