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?