简体   繁体   English

Microsoft SQL Server CE检查表是否存在

[英]Microsoft SQL Server CE Check if table exists

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. 我试图检查Microsoft SQL Server中是否存在表,但不知何故我使用的函数总是返回它不存在,并且输出不指定抛出异常。

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. 显然我在这里缺少一些东西,但我似乎无法找出那是什么。

ExecuteScalar returns the first column of the first row retrieved by the query. ExecuteScalar返回查询检索到的第一行的第一列。
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. 假设您的表确实存在,数据库名称是正确的并且在预期的位置,则返回的行的第一列来自TABLE_CATALOG,即nvarchar列。

Pheraphs you could try to change your query to: Pheraphs您可以尝试将查询更改为:

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..... 说,我还是无法解释为什么当你尝试将ExecuteScalar的返回值转换为int时你没有得到异常.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM