Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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# 如何检查C中是否存在表_C#_Sqlite - Fatal编程技术网

C# 如何检查C中是否存在表

C# 如何检查C中是否存在表,c#,sqlite,C#,Sqlite,首先,让我告诉你们我检查了一系列如何检查表是否存在于。。。。不过,我还需要更多关于这个问题的信息 从sqlite_master中选择名称,其中type='table'和name='table_name'; 我想我必须更改名称sqlite\u master和table\u name,这是我的代码 //公共类SqliteBase的静态函数 公共静态void CreerBasString数据源 { SQLiteConnection=newsqliteconnection; connection.Con

首先,让我告诉你们我检查了一系列如何检查表是否存在于。。。。不过,我还需要更多关于这个问题的信息

从sqlite_master中选择名称,其中type='table'和name='table_name'; 我想我必须更改名称sqlite\u master和table\u name,这是我的代码

//公共类SqliteBase的静态函数 公共静态void CreerBasString数据源 { SQLiteConnection=newsqliteconnection; connection.ConnectionString=数据源=+数据源; 连接。打开; SQLiteCommand=newsqlitecommandconnection; //如果表不存在,则创建该表 command.CommandText=创建表如果不存在beispiel id INTEGER NOT NULL主键自动递增,名称VARCHAR100 NOT NULL;; Console.WriteLineLa Table a bienétécrée; command.ExecuteNonQuery; 指挥、处置; 连接。关闭; 连接、处理; } 以及单元测试功能:

[TestMethod]  
public void LaCreationBaseMarche()
{
    string dataSource = "beispiel.db";
    SqliteBase.CreerBase(dataSource);
    SQLiteConnection connection = new SQLiteConnection();

    connection.ConnectionString = "Data Source=" + dataSource;
    connection.Open();
    SQLiteCommand command = new SQLiteCommand(connection);
    command.CommandText = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'beispiel';";
    SQLiteDataReader reader = command.ExecuteReader();
    Assert.Equals("beispiel", reader[0].ToString());
    reader.Close();
    reader.Dispose();

    command.Dispose();

}
我的问题是:测试方法的command.executeReader返回一个空读取器,当然,当我尝试执行读取器[0]时,我遇到了一个错误。。我是否误用了这个问题


编辑:好的,我想我必须使用文件名^^。现在我改变了它,但它仍然不能工作相同的错误。我还更改了beispel.db中example.db的名称。我更新了我的代码:


提前感谢您的回答:

不,您不必更改sqlite\u主控程序。这是SQLite的元数据表,其中包含有关SQLite已知的所有对象的信息

因此,您的查询将变成:

SELECT name FROM sqlite_master WHERE type='table' AND name='beispiel';
您必须查询sqlite_主表:


从sqlite_master中选择名称,其中type='table'和name='beispiel'

在我看来,你不是从读者那里读到的:

[TestMethod]
public void LaCreationBaseMarche()
{
    string dataSource = "exemple.db";
    SqliteBase.CreerBase(dataSource);
    SQLiteConnection connection = new SQLiteConnection();

    connection.ConnectionString = "Data Source=" + dataSource;
    connection.Open();
    SQLiteCommand command = new SQLiteCommand(connection);
    command.CommandText = "SELECT name FROM exemple WHERE type = 'table' AND name = 'beispiel';";
    SQLiteDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        Assert.Equals("beispiel", reader[0].ToString());
    }

    reader.Close();
    reader.Dispose();
    command.Dispose();
}
编辑


一个可能的问题是数据源。您必须确保这两个方法访问相同的位置。

我执行了以下操作来检查数据库中是否已经存在表

public static bool tableAlreadyExists(SqliteConnection openConnection, string tableName)
{
  var sql = 
  "SELECT name FROM sqlite_master WHERE type='table' AND name='"+tableName +"';"; 
  if(openConnection.State == System.Data.ConnectionState.Open)
  {   
        SqliteCommand command = new SqliteCommand(sql, openConnection);
        SqliteDataReader reader =command.ExecuteReader();
        if(reader.HasRows)
        {
            return true;
        }
        return false;
    }else{
        throw new System.ArgumentException("Data.ConnectionState must be open");
    }
}

好的,我想我必须使用文件名^^。现在我修改了它,但它仍然不能工作。这是一个问题,但主要的问题是我的阅读器实际上没有行^^!我不明白为什么我之前的查询不起作用,方法CreerBase起作用,表被创建了?好的,好问题。。事实上,这正是我想要测试的。。你怎么知道?如果它存在,它应该在哪里?我现在正在研究VisualStudio2015最简单的解决方案:将查询更改为从example中选择name,其中type='table',并调试循环。查看所有结果确定读取器的值仍然为空。但是,我的第一个代码部分出了什么问题?