Recuperare l'elenco delle tabelle presenti in un database SQL

image

 

Nel post precedente si è visto come recuperare l'elenco delle istanze SQL disponibili nella rete locale. E' possibile risalire a queste informazioni utilizzando una query che recupera i dati dalla tabella di sitema sys.sysdatabases.

Abbiamo detto che è possibile evitare l'utilizzo di una query utilizzando il metodo .GetSchema dell'oggetto SqlConnection (versione 2.0 e superiore).

Sempre utilizzando una query o un oggetto SqlConnection è possibile leggere i nomi delle tabelle presenti in un database SQL:

        ' VB.
        ' Recupera l'insieme delle tabelle presenti nel database.
        Dim strSql As String = "Data Source=MIO SERVER;Initial Catalog=MIO DATABASE;Integrated Security=True"
        Using cnn As New SqlClient.SqlConnection(strSql)
            Dim cmd As New SqlClient.SqlCommand("SELECT * FROM sys.tables", cnn)
            cnn.Open()
            Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
            Dim dt As New DataTable
            dt.Load(dr)
            cnn.Close()
            Me.DataGridView1.DataSource = dt
        End Using
{ 
    // C#. 
    // Recupera l'insieme delle tabelle presenti nel database. 
    string strSql = "Data Source=MIO SERVER;Initial Catalog=MIO DATABASE;Integrated Security=True"; 
    using (SqlClient.SqlConnection cnn = new SqlClient.SqlConnection(strSql)) { 
        SqlClient.SqlCommand cmd = new SqlClient.SqlCommand("SELECT * FROM sys.tables", cnn); 
        cnn.Open(); 
        SqlClient.SqlDataReader dr = cmd.ExecuteReader(); 
        DataTable dt = new DataTable(); 
        dt.Load(dr); 
        cnn.Close(); 
        this.DataGridView1.DataSource = dt; 
    } 
} 

 

Utilizzando l'oggetto SqlConnection:

        ' VB.
        ' Recupera l'insieme delle tabelle presenti nel database.
        Dim strSql As String = "Data Source=MIO SERVER;Initial Catalog=MIO DATABASE;Integrated Security=True"
        Using cnn As New SqlClient.SqlConnection(strSql)
            cnn.Open()
            Dim tbl As DataTable = cnn.GetSchema("Tables") ' gestire le restrizioni per filtrare i risultati.
            cnn.Close()
            Me.DataGridView1.DataSource = tbl
        End Using
{ 
    // C#. 
    // Recupera l'insieme delle tabelle presenti nel database. 
    string strSql = "Data Source=MIO SERVER;Initial Catalog=MIO DATABASE;Integrated Security=True"; 
    using (SqlClient.SqlConnection cnn = new SqlClient.SqlConnection(strSql)) { 
        cnn.Open(); 
        DataTable tbl = cnn.GetSchema("Tables"); // gestire le restrizioni per filtrare i risultati.
        cnn.Close(); 
        this.DataGridView1.DataSource = tbl; 
    } 
} 

 

Print | posted on lunedì 21 luglio 2008 10.11

Feedback

# re: Recuperare l'elenco delle tabelle presenti in un database SQL

Left by Stafano P at 03/08/2008 21.49
Gravatar Molto interessante. Proprio quello che mi serviva per un mio progetto.

Your comment:





 
Please add 6 and 4 and type the answer here:

Copyright © Andrea Zingoni

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski