Sql-server – Visual Studio extract database values to excel with header text!

sql servervbavisual studio

I am trying to extract the values from an mdf database to excel, but i can't display the header texts(title fields). I can only view the values. In order to make this work i need a query like this below, but it seems that select with cte
gives me an error.

enter image description here

Best Answer

When you paste code as an image, it's difficult the take that code and rewrite it to show you the correct way to do it. You should always paste your code as text when asking a question.

You need to put the WITH CTE at the beginning of the sql =, such that

sql = ";with cte as (select *, row_number() over()... FROM [TABLE])
SELECT (Your Headers)    
UNION ALL
SELECT (Specific columns) from cte
"