I get a SQL Data Base Connection String from user then I want to check for a particular Table or special stored procedure in the database?
How can I do this in C#?
I get a SQL Data Base Connection String from user then I want to check for a particular Table or special stored procedure in the database?
How can I do this in C#?
 
    
     
    
    The shortcut for this is (in SQL):
SELECT OBJECT_ID('tableName')
or
SELECT OBJECT_ID('storedprocedurename')
If these return null (DBNull.Value), then the item doesn't exist. Otherwise, it does.
So, in C#, that would be something like:
        using (var conn = new SqlConnection(connectionString))
        {
            conn.Open();
            var cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = @"SELECT OBJECT_ID('" + MyObjectName + @"')";
            if (cmd.ExecuteScalar() == DBNull.Value)
            {
                Console.WriteLine("Does not exist");
            }
            else 
            {
                Console.WriteLine("Does exist");
            }
        }
