用于检索 SQL Server 2019 table 列列表的 ExecuteScalar 方法 Visual Studio 2019 c-sharp 的 ASCII 排序不正确

ExecuteScalar method to retrieve SQL Server 2019 table column list in Visual Studio 2019 c-sharp is incorrectly ASCII sorted

我的 SQL 服务器数据库中有一个 table,其结构如下:

CREATE TABLE [file].[NumeTestINV](
    [Category] [nvarchar](255) NULL,
    [Class] [nvarchar](255) NULL,
    [Company] [nvarchar](255) NULL,
    [Division] [nvarchar](255) NULL,
    [Jan] [float] NULL,
    [Feb] [float] NULL,
    [Mar] [float] NULL,
    [Apr] [float] NULL,
    [May] [float] NULL,
    [Jun] [float] NULL,
    [Jul] [float] NULL,
    [Aug] [float] NULL,
    [Sep] [float] NULL,
    [Oct] [float] NULL,
    [Nov] [float] NULL,
    [Dec] [float] NULL
) ON [PRIMARY]

我正在尝试通过 Visual Studio 2019 SSIS 脚本任务中的 C# 代码检索其列列表。我有以下代码 c-sharp 片段。

//Get Matching Column List from SQL Server
string SQLColumnList = "";
SqlCommand cmd = myADONETConnection.CreateCommand();
cmd.CommandText = SQLQueryToGetMatchingColumn;
SQLColumnList = (string)cmd.ExecuteScalar();
MessageBox.Show(" Matching Columns: " + SQLColumnList);

但是,问题是输出的列列表在 ASCII 中排序不正确。我不确定为什么我的 SQLColumnList 没有按照物理上的正确 SQL table 列顺序出现,而是如下所示?

"[Apr],[Aug],[Category],[Class],[Company],[Dec],[Division],[Feb],[Jan],[Jul],[Jun],[Mar],[May],[Nov],[Oct],[Sep]"

我期待以下输出:

"[Category],[Class],[Company],[Division],[Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec]"

这里是 SQLQueryToGetMatchingColumn 的代码,我的来源是一个 Excel 文件,其列列表的顺序与我的 SQL table 相同:

SQLQueryToGetMatchingColumn = "select STUFF((Select  ',['+Column_Name+']' from Information_schema.Columns where Table_Name='" +
                            TableName + "' and Table_SChema='" + SchemaName + "'" +
                            "and Column_Name in (" + @ExcelHeaderColumn + ") for xml path('')),1,1,'') AS ColumnList";

这是 SQLQueryToGetMatchingColumn 的输出:

"select STUFF((Select  ',['+Column_Name+']' from Information_schema.Columns where Table_Name='NumeTestINV' and Table_SChema='file'and Column_Name in ('Category','Class','Company','Division','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec') for xml path('')),1,1,'') AS ColumnList"

CommandText 输出如下:

"select STUFF((Select  ',['+Column_Name+']' from Information_schema.Columns where Table_Name='NumeTestINV' and Table_SChema='file'and Column_Name in ('Category','Class','Company','Division','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec') for xml path('')),1,1,'') AS ColumnList"

我希望这是我代码片段中的一个小代码更改,我不必完全更改检索 SQL 列列表的方法。

如果您希望各列按顺序排列,请尝试这样的操作:

select string_agg( quotename(name), ', ') within group (order by columnproperty(c.object_id, c.name, 'ordinal') ) columnList
from sys.columns c
where c.object_id = object_id('[file].[NumeTestINV]')

select string_agg( quotename(column_name), ', ') within group (order by ordinal_position) columnList
from information_schema.COLUMNS
where TABLE_SCHEMA = 'file'
  and TABLE_NAME = 'NumeTestINV'

或与旧的 XML 串联,

select STUFF((Select  ','+ quotename(Column_Name) from Information_schema.Columns where Table_Name='NumeTestINV' and Table_SChema='file'and Column_Name in ('Category','Class','Company','Division','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec') order by ordinal_position for xml path('')),1,1,'') AS ColumnList