gpt4 book ai didi

c# - C#如何判断一个表是否存在

转载 作者:行者123 更新时间:2023-12-04 16:08:02 24 4
gpt4 key购买 nike

首先,让我告诉你我检查了一堆“如何检查表是否存在于......”。尽管如此,我仍需要有关查询的更多信息

SELECT name FROM sqlite_master WHERE type='table' AND name='table_name';

我想我必须更改名称“sqlite_master”和“table_name”,这是我的代码

// a static function of the public class "SqliteBase"
public static void CreerBase(string dataSource)
{
SQLiteConnection connection = new SQLiteConnection();

connection.ConnectionString = "Data Source=" + dataSource;
connection.Open();
SQLiteCommand command = new SQLiteCommand(connection);

// Create table if it does not exist
command.CommandText = "CREATE TABLE IF NOT EXISTS beispiel ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(100) NOT NULL);";
Console.WriteLine("La Table a bien été créée");
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
connection.Dispose();
}

和单元测试功能:
[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()测试方法返回一个“空”读取器,当然,当我尝试执行 reader[0] 时出现错误。我是否误用了查询?

编辑:好吧,虽然我不得不使用文件名^^。现在我改变了它,但它仍然不起作用(同样的错误)。我还在“beispiel.db”中更改了名称“exemple.db”。我更新了我的代码:)

预先感谢您的回答:)

最佳答案

不,您不必更改 sqlite_master .那是 SQLite 的元数据表,其中包含有关 SQLite 已知的所有对象的信息。

所以你的查询将变成:

SELECT name FROM sqlite_master WHERE type='table' AND name='beispiel';

关于c# - C#如何判断一个表是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45344744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com