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.

6

1 Answer 1

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}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.