如何计算 (1,2,3...) 我的 DataReader 结果
How to numerate (1,2,3...) my DataReader result
所以我有这个代码
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 文件。
这是 select 查询后的结果示例:
+---------------------------------------+
| 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"|
+-------------+-------+--------+--------+
或者换句话说,根据文档编号计算(下一个数字)行。 (select查询按单号排序)。
我认为明智的解决方案是使用 StringBuilder,但我需要一些帮助。
谢谢
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
所以我有这个代码
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 文件。 这是 select 查询后的结果示例:
+---------------------------------------+
| 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"|
+-------------+-------+--------+--------+
或者换句话说,根据文档编号计算(下一个数字)行。 (select查询按单号排序)。
我认为明智的解决方案是使用 StringBuilder,但我需要一些帮助。
谢谢
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