Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting Schema of one Table in C#

Tags:

c#

sql

I want to get Schema for a table with name "Petro" in SQL SErver after initializing connectionString, I use this Code

conn.open();
conn.getSchema("Tables");

but it returns schema for all the tables. I only want the Petro schema. What should I do?

like image 228
Saeed Avatar asked Jun 23 '12 12:06

Saeed


People also ask

How do you create a schema for a table?

In Object Explorer, expand the Databases folder. Expand the database in which to create the new database schema. Right-click the Security folder, point to New, and select Schema. In the Schema - New dialog box, on the General page, enter a name for the new schema in the Schema name box.

What is the schema of a table?

A table schema is a named schema for a set of Query Tables that completely defines the structure of those Query Tables, and ensures that all Query Tables in the set are identically defined. A table schema includes the table structure of a Query Table, as well as its primary index and secondary indices (if any).


2 Answers

You can retrieve the schema in following way:

            string sql = "select * from Petro WHERE 1 = 0";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            DataTable schema = reader.GetSchemaTable();
like image 150
Akash KC Avatar answered Sep 21 '22 09:09

Akash KC


SQL Server solution:

There is a store procedure called sp_columns. When you run that procedure with the table's name as a parameter, it will return the schema just for that table.

like image 34
Sascha Avatar answered Sep 18 '22 09:09

Sascha