简体   繁体   中英

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:

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. I have the following code c-sharp snippet.

//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. 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?

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

I was expecting the following 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 = "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:

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

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

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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