Home > Blockchain >  How to get table columns values from SQL Server database
How to get table columns values from SQL Server database

Time:01-29

var reader = new SqlCommand("Select * from sys.columns", MyCon).ExecuteReader();

This shows this strange output

What I want is to get the columns values (nvarchar, varchar and so on...) from the database table as a string.

This query is not working in SQL Server I tried it

Show Columns From MyTable

it shows

System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'From'.'

CodePudding user response:

Start checking this query:

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS

You can access the data that you want to in this view.

Table name, column name, data type... It's all there.

This is the doc from MS, about the information_schema views:

https://docs.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/columns-transact-sql?view=sql-server-ver15

  •  Tags:  
  • Related