Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查SQL中是否存在表名_C#_Sql_Database_Sql Server Ce - Fatal编程技术网

C# 检查SQL中是否存在表名

C# 检查SQL中是否存在表名,c#,sql,database,sql-server-ce,C#,Sql,Database,Sql Server Ce,在创建新表之前,如何检查表是否已存在 更新代码: private void checkTable() { string tableName = quotenameTxt.Text + "_" + firstTxt.Text + "_" + surenameTxt.Text; string connStr = @"Data Source=|DataDirectory|\LWADataBase.sdf"

在创建新表之前,如何检查表是否已存在

更新代码:

    private void checkTable()
            {

                string tableName = quotenameTxt.Text + "_" + firstTxt.Text + "_" + surenameTxt.Text;
                string connStr = @"Data Source=|DataDirectory|\LWADataBase.sdf";
             //   SqlCeConnection conn = new SqlCeConnection(connStr);
            //    if (conn.State == ConnectionState.Closed) { conn.Open(); }
                using (SqlCeConnection conn = new SqlCeConnection(connStr))
    {
        conn.Open();    
        SqlCeCommand cmd = new SqlCeCommand(@"SELECT * 
                                              FROM INFORMATION_SCHEMA.TABLES 
                                              WHERE TABLE_NAME = @tname", conn);
        cmd.Parameters.AddWithValue("@tname", tableName);
        SqlCeDataReader reader = cmd.ExecuteReader();
        if(reader.Read()){
            MessageBox.Show("Table exists");}
        else{
            MessageBox.Show("Table doesn't exist");
createtable();}

您可以使用SqlClientConnection获取数据库中所有对象的列表

private void checkTable()
{
    string tableName = quotenameTxt.Text + "-" + firstTxt.Text + "-" + surenameTxt.Text;
    string connStr = @"Data Source=|DataDirectory|\LWADataBase.sdf";

    using (SqlCeConnection conn = new SqlCeConnection(connStr))
    {
        bool isTableExist = conn.GetSchema("Tables")
                                .AsEnumerable()
                                .Any(row => row[2] == tableName);
    }

    if (!isTableExist)
    {
        MessageBox.Show("No such data table exists!");
    }
    else
    {
        MessageBox.Show("Such data table exists!");
    }
}

来源:

Sql Server Compact支持这些视图

编辑 在版本3.5中,似乎不接受TOP 1指令。但是,给定WHERE子句,使用它与否应该没有区别,要使其工作,只需将查询更改为

SqlCeCommand cmd = new SqlCeCommand(@"SELECT * FROM INFORMATION_SCHEMA.TABLES 
                                      WHERE TABLE_NAME = @tname", conn);
第二次编辑 查看创建表的代码。
(在聊天中,为了完整性,我建议将其添加到问题中)


tableName变量周围的单引号将成为表名的一部分。但是表exists的检查不使用引号。您的代码将通过尝试再次创建带引号的表的路径。只需删除名称周围的引号。不需要它们。

的可能重复是否需要表架构?的可能重复分析查询时出错。[Token line number=1,Token line offset=12,Token in error=1]是我从该代码中得到的错误。我使用Sql Server Compact 4.0对数据库进行测试时没有错误。现在,我无法使用3.5进行测试。但是,请尝试删除
TOP 1
,只保留
SELECT*FROM INFORMATION\u SCHEMA.TABLES...
这应该是绝对正确的,除非3.5中不支持信息\u SCHEMA视图。Ok刚刚下载并用3.5位测试过。这是第一名。移除它,它应该可以很好地工作。我已经按照你说的做了,但实际上它什么都没做。不管该表是否存在,它只会给我一个“表不存在”消息框。“不支持指定的方法”在版本4.0中添加了“GetSchema”
SqlCeCommand cmd = new SqlCeCommand(@"SELECT * FROM INFORMATION_SCHEMA.TABLES 
                                      WHERE TABLE_NAME = @tname", conn);
using (SqlCeCommand command = new SqlCeCommand( 
        "CREATE TABLE ['" + tableName + "'] " + 
        "(Weight INT, Name NVARCHAR, Breed NVARCHAR)", con))