Syntax error in Transact-SQL inside a C# code at runtime

  • Thread starter Thread starter MyCatAlex
  • Start date Start date
M

MyCatAlex

Guest
I get an error (Exception) while running this code:

private void ConnectToMaster()
{
List<SqlServer.DbDataRecord> databases = new List<SqlServer.DbDataRecord>();
Service service = new Service();
Server srv = service.GetServer("Master");
using (SqlConnection conn = new SqlConnection
(srv.ConnectionContext.ConnectionString))
{
conn.Open();
SqlCommand cmdm = new SqlCommand();
cmdm.Connection = conn;
cmdm.CommandType = CommandType.Text;
cmdm.CommandText = "SELECT * FROM [master].[sys].[sp_databases]"; <= ERROR
cmdm.Parameters.Clear();
try
{
using (SqlDataReader rdr = cmdm.ExecuteReader(CommandBehavior.SequentialAccess))
{
if (rdr.HasRows)
{
foreach (System.Data.Common.DbDataRecord row in rdr)
{
databases.Add((SqlServer.DbDataRecord)row);
}
} // if (rdr.HasRows)
}
}
catch (Exception ex)
{
Console.WriteLine("{0}, \r\n {1}, \r\n {2}",
ex.Message, ex.InnerException, ex.StackTrace);
};
}
} // ConnectToMaster


The error is "NullReferenceException." How can I correct it?

I actually need to run this Trans-SQL and get the databases as a result.

Thanks, - MyCatAlex

Continue reading...
 
Back
Top