Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:07:59+00:00 2026-06-12T19:07:59+00:00

I am trying to check if a table exists in Microsoft SQL Server, but

  • 0

I am trying to check if a table exists in Microsoft SQL Server, but somehow the function I use always returns that it doesn’t exist, and the output doesn’t specify that an exception was thrown.

This occurs both before I created the table (which is expected), as well as after I created the table (which is not expected).

This is the function I am using:

/// <summary>
/// Checks  if a certain Sql Server Table exists
/// </summary>
/// <param name="_databasename">The name of the database</param>
/// <param name="_password">The password of the database</param>
/// <param name="_tablename">The name of the table to check</param>
/// <returns>
///     'true' if table exists
///     'false' if table not exists or if an exception was thrown
/// </returns>
public Boolean TableExists(String _databasename, String _password, String _tablename)
{
    if (!_databasename.Contains(".sdf")) { _databasename = _databasename + ".sdf"; }
    try
    {
        String connectionString = "DataSource=" + _databasename + "; Password=" + _password;
        SqlCeConnection conn = new SqlCeConnection(connectionString);

        if (conn.State==ConnectionState.Closed) { conn.Open(); }

        using (SqlCeCommand command = conn.CreateCommand())
        {
            command.CommandType = CommandType.Text;
            command.CommandText = "SELECT * FROM Information_Schema.Tables WHERE TABLE_NAME = '" + _tablename + "'";
            Int32 count = Convert.ToInt32(command.ExecuteScalar());

            if (count == 0)
            {
                Debug.WriteLine("Table " + _tablename + " does not exist.");
                return false;
            }
            else
            {
                Debug.WriteLine("Table " + _tablename + " exists.");
                return true;
            }
        }
    }
    catch(Exception _ex)
    {
        Debug.WriteLine("Failed to determine if table " + _tablename + " exists: " + _ex.Message);
        return false;
    }
}

There clearly is something I am missing here, but I just can’t seem to find out what that is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:08:00+00:00Added an answer on June 12, 2026 at 7:08 pm

    ExecuteScalar returns the first column of the first row retrieved by the query.
    Supposing that your table really exist, the database name is correct and in the expected location, then the first column of the row returned is from TABLE_CATALOG, a nvarchar column.

    Pheraphs you could try to change your query to:

    command.CommandText = "SELECT COUNT(*) FROM Information_Schema.Tables " + 
                          "WHERE TABLE_NAME = .......";
    

    Said that, I still can’t explain why you don’t get an exception when you try to convert to an int the return value of ExecuteScalar…..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to write an SQL query that will check in the table ‘persons’
I'm trying to check if a file exists. If I use this: NSData *data
I'm trying to check to see if a table exists in and SQLite Database
I am trying to make simple java code that will check if a table
I'm using cakephp and trying to check if a table has data entered in
I'm trying to write a simple stock check program, and I have a Table
I am trying to create a check constraint on an access (jet?) table. So,
I am trying to check if an email address already exists in a user
I'm trying to create a function that, when run, creates numerical ID, checks to
I am trying to check whether an entry exists (one or more) in our

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.