0
            var conn = new SqlConnection("");

            try
            {
                conn.Open();
                string s = $"SELECT Email FROM {tableName} WHERE {query}";
                SqlCommand cmd = new SqlCommand(s, conn);
                SqlDataReader reader = cmd.ExecuteReader();

                int email = reader.GetOrdinal("Email");                

                while (reader.Read())
                {
                    var response = new User
                    {                       
                        Email = reader.IsDBNull(email) ? null : reader.GetString(email)
                    };
                    emailList.Add(response);
                }

                reader.Close();
            }
            finally
            {
                conn.Close();
            }

How do I update this code to verify if a table with name {tableName} exists in sql database before executing this code.

user989988
  • 3,006
  • 7
  • 44
  • 91

1 Answers1

1

Run the following query and substitute {tablename} with the name of the table, you are looking for:

SELECT name, SCHEMA_NAME(schema_id) AS [schema], create_date, object_id, modify_date FROM sys.tables WHERE name = {tablename}

Mohammad Mirmostafa
  • 1,720
  • 2
  • 16
  • 32