I did not create and do not own this database. My job is usually just to put data in it, so I don't have to think too much about the DBA side. I am messing with a side project and can not figure out why I can't list all the table names. I have found several methods to gather this information, but none work for me in a c# context. I get 0 rows back every time. They do work when executed as queries in azure data studio. I get 99 rows back (the correct number of tables) no matter which method I use to enumerate the tables. My connection string specifies the database to use within the server.
The query named workingQuery returns the expected information (1 row with 15 fields). So I know my connection string is correct and my code is functional.
I have a test function that I am trying many queries on.
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
List<string> fields = new List<string>();
string workingQuery = "SELECT TOP (1) * FROM myRealTable";
string failingTablesQuery1 = "SELECT name FROM sysobjects WHERE xtype = 'U'";
string failingTablesQuery2 = "SELECT name AS table_name FROM sys.tables GO";
string failingTablesQuery3 = "SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_TYPE = 'BASE TABLE'";
using (var command = conn.CreateCommand())
{
command.CommandText = workingQuery;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
for (int i = 0; i < reader.FieldCount; i++)
{
fields.Add(reader[i].ToString());
}
conn.Close();
return fields;
}
}
In addition to the raw sql queries I have also tried SqlConnection.GetSchema("Tables"). It also returns no rows.
I tried prepending USE myDatabaseName; to the front of the queries to no effect.
I can only assume this is an issue with the way the database is set up. Can anyone help me track down what is causing this issue?
SOLUTION: My connection string had credentials that did not have the necessary permissions for this operation.