简体   繁体   English

Visual Studio 2019 c-sharp 中检索 SQL Server 2019 表列列表的 ExecuteScalar 方法错误地按 ASCII 排序

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

I have a table in my SQL Server database with the following stucture:我的 SQL 服务器数据库中有一个表,其结构如下:

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]

I am trying to retrieve its column list via C# code in a Visual Studio 2019 SSIS Script Task.我正在尝试通过 Visual Studio 2019 SSIS 脚本任务中的 C# 代码检索其列列表。 I have the following code c-sharp snippet.我有以下代码 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);

However, the problem is that the column list is being outputted incorrectly sorted in ASCII.但是,问题在于列列表输出的 ASCII 排序不正确。 I am not sure why my SQLColumnList is not coming out with the correct SQL table column order as it is physically and, instead is is coming out as the following?我不确定为什么我的 SQLColumnList 没有以正确的 SQL 表列顺序出现,因为它是物理上的,而是如下所示?

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

I was expecting the following output:我期待以下 output:

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

Here is the code for SQLQueryToGetMatchingColumn, my source is an Excel file with the same column list in the same order as my physical SQL table:这是 SQLQueryToGetMatchingColumn 的代码,我的源是一个 Excel 文件,其列列表与我的物理 SQL 表的顺序相同:

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";

Here is the output for SQLQueryToGetMatchingColumn:这是 SQLQueryToGetMatchingColumn 的 output:

"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"

The CommandText output is the following: CommandText output 如下:

"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"

I am hoping that this is a small code change in my snippet and that I don't have to completely change the method by which I am retrieving my SQL Column list.我希望这是我的代码片段中的一个小代码更改,并且我不必完全更改检索 SQL 列列表的方法。

If you want the columns in order, try something like this:如果您希望列按顺序排列,请尝试以下操作:

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]')

or或者

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'

or with the old XML conatenation,或与旧的 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM