Search code examples
c#sqlsql-servermetadatagetschema

Can GetSchema method work asynchronously?


How does GetSchema method of SqlConnection class work? Does it run queries? Can it be called asynchronously?


Solution

  • Assuming you want the c# answer since it is in your tag see: Use SqlConnection.GetSchema to get Tables Only (No Views)

        using System.Data.SqlClient;
    

    and

        SqlConnection.GetSchema("Tables");
    

    or

        SQLCon.Open();
        DataTable tables = SQLCon.GetSchema("Tables");
        SQLCon.Close();
    

    I would guess what this is actually doing is running a query to either the sys tables or the information_schema.tables in SQL when you open the connection. something like:

       SELECT * FROM information_schema.tables 
    

    or

        SELECT * FROM [database].sys.tables
    

    And the equivalent calls for the other methods within the class.

    For Async calls you can use

        Asynchronous Processing=True; 
    

    in your connection string

        string connectionString = "Data Source=yourDataSource;Initial Catalog=yourCat;Integrated Security=true;Asynchronous Processing=True;";
    

    Did this answer your question?