1

I'm trying to get a list of databases using the following code:

DataTable databases = conn.GetSchema("Databases");

For an SQL Server.

That definitely works as it has worked on other instances. But it's not returning any databases at all on one instance, even though there are definitely DBs in it.

So, what permissions/security rights are necessary to pull the schema?

2
  • how does your connection string look like?
    – CeOnSql
    Jul 14, 2015 at 15:19
  • Then you surely don't have any databases for that instance. Check again.
    – Rahul
    Jul 14, 2015 at 15:45

1 Answer 1

2

Using SQL Server Profiler, calling GetSchema("Databases") causes the following SQL to be sent to the server:

exec sp_executesql
  N'IF OBJECT_ID(''master..sysdatabases'') IS NULL
      EXEC sp_executesql N''select name as database_name, dbid, crdate as create_date
                            from sysdatabases
                            where (name = @Name or (@Name is null))'',
                         N''@Name NVARCHAR(128)'',
                         @Name=@Name
    ELSE
      EXEC sp_executesql N''select name as database_name, dbid, crdate as create_date
                            from master..sysdatabases
                            where (name = @Name or (@Name is null))'',
                         N''@Name NVARCHAR(128)'',
                         @Name=@Name',
  N'@Name nvarchar(4000)',
  @Name=NULL

For this SQL statement to run successfully, permissions are needed to execute the sp_executesql stored procedure, execute SELECT statements, and access the sysdatabases view.

sp_executesql only needs the login account to have membership in the public role.

sysdatabases maps to sys.databases, which has its own set of permissions.

SELECT also has its own set of permissions.

It's probable the login account hasn't been granted the ability to view sys.databases, or execute SELECT statements.

One way to narrow down the problem is to catch any exceptions and examine the InnerException property:

using System;
using System.Collections;
using System.Data.SqlClient;

namespace TestProgram
{
  public class Program
  {
    public static void Main(String[] args)
    {
      using (var connection = new SqlConnection("Server=(local);Database=my_database;Trusted_Connection=True;"))
      {
        connection.Open();

        try
        {
          var databases = connection.GetSchema("Databases");
        }
        catch (Exception ex)
        {
          Console.WriteLine(GetAllExceptionMessages(ex));
        }
      }
    }

    /* Recursively build a string of all nested exception data, stack traces
       and messages. */
    private static String GetAllExceptionMessages(Exception ex)
    {
      if (ex == null)
      {
        return "";
      }
      else
      {
        var nl = Environment.NewLine;
        var result = ex.Message + nl;

        foreach (DictionaryEntry de in ex.Data)
          result += String.Concat("  ", de.Key, ": ", de.Value, nl);

        /* StackTrace might be null when running this code in NUnit. */
        if (ex.StackTrace != null)
          result += String.Format("{0}Stack Trace:{0}{1}", nl, ex.StackTrace.ToString());

        return (String.Concat(result, nl, GetAllExceptionMessages(ex.InnerException))).Trim();
      }
    }
  }
}

Another option is to use SQL Server Profiler and examine the traffic between your app and SQL Server. This might also pinpoint what permissions are necessary call GetSchema() successfully.

Ultimately, you will have to contact the DBA in charge of the problematic SQL Server instance to be granted the necessary permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.