Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Yogeshwer Sharma 10 posts 80 karma points
    Aug 09, 2024 @ 17:57
    Yogeshwer Sharma
    0

    Doesn anyone know how to check if Table Exist in Database or not

    I am using Umbraco 8.18.12 and trying to get some data from table but in case table is not there in DB getting error

     using (var scope = serviceProvider.CreateScope(autoComplete: true))
                    {
                        var sql = scope.SqlContext.Sql().Select("*").Where<Item>(x => x.id == itemId);
                        var record = scope.Database.SingleOrDefault<Item>(sql);
                        if (record != null)
                        {
                            test = true;
                        }
                    }
    

    Doesn anyone know how I can check if table exist here ?

  • Adam Hearn 16 posts 127 karma points
    Aug 10, 2024 @ 06:54
    Adam Hearn
    0

    Pretty sure there's no such function available in EF but you've got the following options:

    1. try catch around the code block (should be DB agnostic)

    and the following will be SQL Server specific

    1. perform a query of SELECT OBJECT_ID('Item'); a null will mean there is no table [called Item]
    2. perform a query of SELECT COUNT(*) FROM sys.tables st WHERE st.name = 'Item' and check for 0 or 1 response

    If you go for option 2 or 3 you may want to wrap those up in an extension method/helper.

  • Yogeshwer Sharma 10 posts 80 karma points
    Aug 12, 2024 @ 08:09
    Yogeshwer Sharma
    0

    Thanks Adam will try these solutions.

Please Sign in or register to post replies

Write your reply to:

Draft