Obtain all tables in the database (C # implementation)

Source: Internet
Author: User
Body: Obtain all tables in the database
After logging out of this blog, I was noticed by everyone. Among them, "shengguojun" suggested that using the stored procedure "sp_tables" can also be used to achieve this goal. I have perfected this blog.

In many cases, we need to list all the tables in the specified database. When using C # for software development, what methods can we achieve this goal? I have summarized this in the following 6 ways to achieve this goal.

1. sqldmo
Sqldmo is an ideal way to operate sqlserver. If your database is sqlserver, you can consider using this method. To use sqldmo in C #, you need to add reference to sqldmo, and then using sqldmo in the current file; that is, you can use sqldmo. You can obtain the object model of sqldmo with the help of sqlserver.
Private void gettabels_dmo (string strservername, string struser, string strpwd, string strdatabase)
{
Sqldmo. sqlserver Server = new sqldmo. sqlserverclass ();
// Connect to the server
Server. Connect (strservername, struser, strpwd );
// Traverse all databases to obtain the specified database
For (INT I = 0; I <server. Databases. Count; I ++)
{
// Determine whether the current database is a specified database
If (server. Databases. Item (I + 1, "DBO"). Name = strdatabase)
{
// Obtain the specified database
Sqldmo. _ database DB = server. Databases. Item (I + 1, "DBO ");
// Obtain all tables in the specified database
For (Int J = 0; j <dB. Tables. Count; j ++)
{
MessageBox. Show (db. Tables. Item (J + 1, "DBO"). Name );
}
}
}
}

2. ADOX

ADOX is ADO extensions for DDL and security. It is an extension of Microsoft's ADO technology. We can use it to operate the database structure. It is a COM component. It is estimated that some ADOX functions will be added in ADO. net in the future. If you need some ADOX materials, I can provide them. The following example uses ADOX to obtain all tables of the current database.
Private void gettables_adox ()
{
// ADO database connection
ADODB. connectionclass Cn = new ADODB. connectionclass ();
String connectionstring = "provider = sqloledb.1; Integrated Security = sspi; initial catalog = test; Data Source = hbxp ";
CN. Open (connectionstring, "sa", "", 0 );
// Operate the catox object of ADOX
Catalogclass cat = new catalogclass ();
Cat. activeconnection = cn;
For (INT I = 0; I <cat. Tables. Count; I ++)
{
MessageBox. Show (Cat. Tables [I]. Name );
}
}

Note:CodeIn cat. activeconnection cannot be the connection in ADO. net, but it should be the connection of ADO.

3. oledbconnection In ADO. net

In C #, we will first consider using ADO. Net to solve the problem. If there is no way, we will consider using ADOX or sqldmo to solve this problem. Although ADOX and sqldmo can solve this problem, they are COM components after all. They are not very easy to use in. NET and may differ from non-. Net platforms. The following example shows the method getoledbschematable of oledbconnection In ADO. Net to obtain the database architecture. You can see the description of this method in msdn:
Public datatable getoledbschematable (
Guid schema,
Object [] restrictions );
Parameters
Schema
Oledbschemaguid value, which specifies the schema table to be returned.
Restrictions
Array of objects with limit values. These values are applied in the order of restricted columns. That is, the first limit value is applied to the first limit column, the second limit value is applied to the second limit column, and so on.
Return Value
The datatable that contains the requested schema information.
For more information, see msdn. The following example shows how to implement it.
Private void gettables_adonet ()
{
// Process oledbconnection
String strconnectionstring = @ "Integrated Security = sspi; Data Source = hbxp; initial catalog = test; provider = sqloledb.1 ";
Oledbconnection Cn = new oledbconnection (strconnectionstring );
CN. open ();
// Use oledbconnection's getoledbschematable to obtain the database structure
Datatable dt = cn. getoledbschematable (oledbschemaguid. Tables, new object [] {null, "table "});
Foreach (datarow DR in DT. Rows)
{
MessageBox. Show (string) Dr ["table_name"]);
}

}

4. Information Architecture View

Information Architecture views are architectural views defined in SQL-92 standards that are independent of system tables. The biggest advantage of the Information Architecture view is that even if we make important changes to the system table Program You can also access these views normally. The following example uses the information architecture view.
Private void gettables_information_schema ()
{
// Open the connection
String strconnectionstring = system. configuration. configurationsettings. etettings ["connectionstring"];
Sqlcn = new sqlconnection (strconnectionstring );
Sqlcn. open ();
// Use the information architecture View
Sqlcommand sqlcmd = new sqlcommand ("select table_name from information_schema.tables where table_type = 'base table'", sqlcn );
Sqldatareader DR = sqlcmd. executereader ();
While (dr. Read ())
{
MessageBox. Show (dr. getstring (0 ));
}
}

5. Use System tables

If your database system is sqlserver, you can use the following method to obtain all the tables of the current database:
Private void gettables_systemtable ()
{
// Open the connection
String strconnectionstring = system. configuration. configurationsettings. etettings ["connectionstring"];
Sqlcn = new sqlconnection (strconnectionstring );
Sqlcn. open ();
// Use the information architecture View
Sqlcommand sqlcmd = new sqlcommand ("select object_name (ID) from sysobjects where xtype = 'U' and objectproperty (ID, 'ismsshipped ') = 0", sqlcn );
Sqldatareader DR = sqlcmd. executereader ();
While (dr. Read ())
{
MessageBox. Show (dr. getstring (0 ));
}
}

6. Use the SQL Server Stored Procedure "sp_tables"
The following is the supplementary code of the stored procedure method proposed by a friend of shengguojun.
Public void gettables_storedprocedure ()
{
// Process oledbconnection
String strconnectionstring = @ "Integrated Security = sspi; Data Source = hbxp; initial catalog = test; provider = sqloledb.1 ";
Oledbconnection Cn = new oledbconnection (strconnectionstring );
CN. open ();
// Execute the Stored Procedure
Oledbcommand cmd = new oledbcommand ("sp_tables", CN );
Cmd. commandtype = commandtype. storedprocedure;
Oledbdatareader DR = cmd. executereader ();
While (dr. Read ())
{
MessageBox. Show (Dr ["table_name"]. tostring ());
}
}

Summary: The methods used to obtain all the tables in the current database are not limited to those listed by myself. I hope the above methods can serve as a reference. The above methods have their own advantages and disadvantages. I hope you can flexibly handle them and tell me the new methods you have found. Thank you.

Category: C #. net Release date:

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.