Another way but worth mentioning: The API contained in Microsoft.SqlServer.Smo.dll makes it very to access database:
private IEnumerable<string> getAllTables()
{
var sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
var serverConnection = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection);
var server = new Microsoft.SqlServer.Management.Smo.Server(serverConnection);
var database = server.Databases[databaseName];
foreach (Microsoft.SqlServer.Management.Smo.Table table in database.Tables)
{
yield return table.Name;
}
}
The coolest thing is that Microsoft.SqlServer.Management.Smo.Table object allows you to perform all kinds of operations, like changing schema, scripting, etc...
9条答案
按热度按时间ozxc1zmp1#
It is as simple as this:
where
_conn
is a SqlConnection object that has already been connected to the correct database.ne5o7dgx2#
Just another solution:
5gfr0r5j3#
Run a sql command for:
vzgqcmou4#
I am using this
ExtensionMethod
forSqlConnection
:gtlvzcf85#
If you want to get all table names from a database you can do something like this ;
Get all databases using the other response and create a connection to each and use function "GetAllTables" to get all table names from that db.
pu82cl6c6#
Another way but worth mentioning: The API contained in Microsoft.SqlServer.Smo.dll makes it very to access database:
The coolest thing is that Microsoft.SqlServer.Management.Smo.Table object allows you to perform all kinds of operations, like changing schema, scripting, etc...
nuypyhwy7#
See How to get a list of SQL Server databases for one way:
xbp102n08#
My version of yonexbat answer
moiiocjp9#
Thanks to Slugster for his answer. Here is an expanded explanation of how to view a list of the tables in an database.
in an asp.net form add the following:
then in C# code behind add the following function:
not forgetting to add
and
at the top of you page / inside your parent class.
With inside your Page_Load:
connString is a class file (called connectionClass.cs) that is stored in the App_Code folder
then finally in web.config
for example