Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.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#重新打开程序后未加载sqlite_C#_Database_Visual Studio_Sqlite_System.data.sqlite - Fatal编程技术网

c#重新打开程序后未加载sqlite

c#重新打开程序后未加载sqlite,c#,database,visual-studio,sqlite,system.data.sqlite,C#,Database,Visual Studio,Sqlite,System.data.sqlite,我有一个用c#编写的简单程序,我正在使用SQLite保存配置文件列表(电子邮件、密码)。一切都很好,直到我关闭程序,然后重新打开它。当我这样做的时候,桌子是空的。这段代码位于我的表单构造函数中,它在程序加载时首先触发(它是一个单表单程序,非常基本)。我正在使用System.Data.SQLite库。我可以在我的项目文件夹(bin/debug/)中看到该文件。有人能解释一下为什么这些信息没有被保存,并且在重新打开程序时可以阅读吗 SQLiteConnection.CreateFile("MyDat

我有一个用c#编写的简单程序,我正在使用SQLite保存配置文件列表(电子邮件、密码)。一切都很好,直到我关闭程序,然后重新打开它。当我这样做的时候,桌子是空的。这段代码位于我的表单构造函数中,它在程序加载时首先触发(它是一个单表单程序,非常基本)。我正在使用System.Data.SQLite库。我可以在我的项目文件夹(bin/debug/)中看到该文件。有人能解释一下为什么这些信息没有被保存,并且在重新打开程序时可以阅读吗

SQLiteConnection.CreateFile("MyDatabase.db");
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.db;Version=3;");
m_dbConnection.Open();
string sql = "CREATE TABLE " + profileTable + " (" + emailCol + " VARCHAR(320), " + passwordCol + " VARCHAR(30))";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "SELECT * FROM "+profileTable+" order by "+emailCol+" desc";
command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    emailProfileListBox.Items.Add(reader[emailCol] as String);
}
这是我的INSERT语句,它执行INSERT操作,并且已经过验证

string sql1 = "INSERT INTO "+profileTable+" ("+emailCol+", "+passwordCol+") VALUES (\'"+from_email_address.Text+"\', \'"+passwordTextBox.Text+"\')";
SQLiteCommand command1 = new SQLiteCommand(sql1, m_dbConnection);
command1.ExecuteNonQuery();

可能是因为您在重新运行程序时正在创建表吗?

第一行,如果您每次运行新文件时都创建新文件,则该文件将被覆盖

SQLiteConnection.CreateFile("MyDatabase.db");
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.db;Version=3;");
m_dbConnection.Open();
string sql = "CREATE TABLE " + profileTable + " (" + emailCol + " VARCHAR(320), " + passwordCol + " VARCHAR(30))";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "SELECT * FROM "+profileTable+" order by "+emailCol+" desc";
command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    emailProfileListBox.Items.Add(reader[emailCol] as String);
}
SQLiteConnection.CreateFile("MyDatabase.db");
将创建语句包装在
if
块中,而不是检查磁盘上是否存在该文件

if (!System.IO.File.Exists("MyDatabase.db")) 
{
   SQLiteConnection.CreateFile("MyDatabase.db");
   // continue your creation script here
}

在指定路径中创建或覆盖数据库文件。这只是创建了一个零字节的文件,当文件正确打开时,SQLite会将该文件转换为数据库。请注意,现有文件将被覆盖

SQLiteConnection.CreateFile("MyDatabase.db");

旁注
  • 您还应该在
    中使用
    块包装您的连接和该类型实现
    IDisposable
    的任何其他实例,以确保始终释放外部资源
  • 对于传递给它们的任何值,都应该使用参数化语句。因此,选择中的插入、更新和条件应使用参数传递值
  • 永远不要存储密码,即使是加密的。改为创建密码的单向盐渍散列并存储它。有很多现有的库/代码片段可以为您做到这一点

SQLiteConnection.CreateFile(“MyDatabase.db”)您还可以检查表的存在
conn.Execute($“CREATE TABLE IF NOT executes{TableName}({TableDefinition})”)
谢谢,至于密码,您也是正确的,但在这种情况下,安全性并不重要,也不重要。这只是一个非常简单的程序,我为我的工作,永远不会看到我的办公室以外的任何东西。再次感谢您,祝您一切顺利!知道了。这里没有