C# Microsoft SQL Server CE检查表是否存在

C# Microsoft SQL Server CE检查表是否存在,c#,sql-server-ce,C#,Sql Server Ce,我试图检查Microsoft SQL Server中是否存在表,但不知何故,我使用的函数总是返回它不存在,并且输出没有指定引发了异常 这发生在我创建表之前(这是预期的),也发生在我创建表之后(这不是预期的) 这是我正在使用的函数: /// <summary> /// Checks if a certain Sql Server Table exists /// </summary> /// <param name="_databasename">The na

我试图检查Microsoft SQL Server中是否存在表,但不知何故,我使用的函数总是返回它不存在,并且输出没有指定引发了异常

这发生在我创建表之前(这是预期的),也发生在我创建表之后(这不是预期的)

这是我正在使用的函数:

/// <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;
    }
}
//
///检查某个Sql Server表是否存在
/// 
///数据库的名称
///数据库的密码
///要检查的表的名称
/// 
///如果表存在,则为“true”
///如果表不存在或引发异常,则为“false”
/// 
公共布尔表存在(字符串\u数据库名、字符串\u密码、字符串\u表名)
{
如果(!\u databasename.Contains(“.sdf”){u databasename=\u databasename+“.sdf”;}
尝试
{
字符串连接String=“DataSource=“+\u databasename+”;Password=“+\u Password;
SqlCeConnection conn=新的SqlCeConnection(connectionString);
如果(conn.State==ConnectionState.Closed){conn.Open();}
使用(SqlCeCommand=conn.CreateCommand())
{
command.CommandType=CommandType.Text;
command.CommandText=“从信息_Schema.Tables中选择*,其中TABLE_NAME=”+_tablename+“”;
Int32 count=Convert.ToInt32(command.ExecuteScalar());
如果(计数=0)
{
Debug.WriteLine(“表”+_tablename+“不存在”);
返回false;
}
其他的
{
WriteLine(“表”+_tablename+“存在”);
返回true;
}
}
}
捕获(例外情况)
{
Debug.WriteLine(“无法确定表”+\u tablename+”是否存在:“+\u ex.Message”);
返回false;
}
}
很明显,我在这里缺少了一些东西,但我似乎无法找到这是什么。

返回查询检索到的第一行的第一列。
假设您的表确实存在,数据库名称正确并且位于预期的位置,那么返回的行的第一列来自table_CATALOG,一个nvarchar列

您可以尝试将查询更改为:

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

尽管如此,我仍然无法解释为什么在尝试将ExecuteScalar的返回值转换为int时没有出现异常…

这对我仍然不起作用。它不断返回不存在的表。然后您应该检查数据库名称。您是否正在使用Visual Studio并将sdf文件的副本始终设置为true?我正在使用SharpDevelop,并为我的项目使用私有数据库。至于抄袭,则一直如此;我没有将它设置为true,但我不知道它是否默认设置为true。我不知道SharpDevelop。我甚至不知道拷贝是否总是存在。在VS中,如果此属性为true,则每次编译项目时,文件都会复制到工作调试目录。当然,此新副本将销毁对工作副本所做的任何更改。您是否检查了传入的路径是否正确?如果您的意思是数据库文件是否存在于我的调试文件夹中,则是。但它并不是每次我运行应用程序时都复制到那里。它检查数据库文件是否存在,如果存在,则不执行任何操作。