Obtain the SQL Server express database structure

Source: Internet
Author: User
Tags sql server express

In. net1.0, you can connect to the database by using oledb to obtain various attributes of the database, which has been used before, no problem (http://dlwang2002.cnblogs.com/archive/2005/04/11/207996.html)
However, this method does not support SQL Server express operations, but only supports sqlconnection.
Http://www.microsoft.com/china/msdn/library/data/dataaccess/daadonet2schemas.mspx.

According to the above introduction, I wrote several tests as follows:

Sqlconnection connection =   Null ;
Connection =   New Sqlconnection (connectstring );
Connection. open ();
Datatable tables = Connection. getschema ( " Tables " );

This sectionCodeYou can obtain all data tables of a database.

String [] Col =   New   String [ 4 ]; /**/ ///For colums {"table_catalog", "table_schema", "table_name", "column_name", "ordinal_position", "column_default "}
For ( Int I =   0 ; I <   4 ; I ++ )
Col [I] =   Null ;
Ol [ 2 ] = Tablename;
Dataview Columns = Connection. getschema ( " Columns " , Col). defaultview;

The code above can get all columns of a specified table. Note that the second parameter of getschema is a restriction. The third column specifies the name of the data table.

// Connection. getschema (dbmetadatacollectionnames. metadatacollections). writexml ("meta. xml ");
Datatable keyfields = Connection. getschema ( " Indexcolumns " , New   String [] {Null,Null, Tablename} );
Int Index = 0 ;
For ( Int I =   0 ; I < Keyfields. Rows. Count; I ++ )
{
If (Keyfields. Rows [I] [ 2 ]. Tostring (). indexof ( " Pk _ " ) >   - 1 )
Index = I;
}
Return Global. Clean (keyfields. Rows [Index] [ 6 ]. Tostring ());

This section is used to obtain the primary key. It seems that there is no primarykeys keyword in it. It can only be used in this way.


Datatable keyfields = Connection. getschema ( " Foreignkeys " , New   String [] {Null,Null, Tablename} );

This section is used to obtain foreign keys. However, this section cannot be used together.
This is the output result. < Foreignkeys >
< Constraint_catalog > D: \ XX \ bin \ debug \ aspnetdb. MDF </ Constraint_catalog >
< Constraint_schema > DBO </ Constraint_schema >
< Constraint_name > FK _ aspnet_us _ appli _ 01_a276 </ Constraint_name >
< Table_catalog > D: \ XX \ bin \ debug \ aspnetdb. MDF </ Table_catalog >
< Table_schema > DBO </ Table_schema >
< Table_name > Aspnet_users </ Table_name >
< Constraint_type > Foreign key </ Constraint_type >
< Is_deferrable > No </ Is_deferrable >
< Initially_deferred > No </ Initially_deferred >
</ Foreignkeys >

It is pointed to here to get "fk_table_name", "fk_column_name", "pk_table_name", "pk_column_name", but it seems difficult to find these parameters. How should I obtain it?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.