无法将记录插入到数据库 C# SQLite [英] Can't insert the record to database C# SQLite
本文介绍了无法将记录插入到数据库 C# SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在为与数据库相关的错误而苦苦挣扎.基本上我有一个 SQLite 数据库,我想插入数据,但是在我执行该方法后,没有数据写入数据库,但也没有显示错误.这是我的代码:数据库连接类:
I've been struggling with an error related to the database. Basically I have an SQLite db and I want to insert the data, but after I execute the method, no data is written to the db but no errors are shown either. This is my code: The db connection class:
class SqlDb
{
public static SQLiteConnection SqLiteConnection;
public static string DATABASE_WAREHOUSE = "DaneDB.db";
public static string DATABASE_LACZKA =
$"Data Source= {DATABASE_WAREHOUSE};Version=3;New=False;Compress=True;";
public SQLiteConnection Connect()
{
try
{
SqLiteConnection = new SQLiteConnection(DATABASE_LACZKA);
SqLiteConnection.Open();
}
catch (Exception e)
{
MessageBox.Show($"Could not connect to the database {e}");
throw;
}
return SqLiteConnection;
}
public void Disconnect()
{
SqLiteConnection.Close();
}
}
和插入方法
private void LoadToDb_Click(object sender, EventArgs e)
{
Data modelData = new Data();
SqlDb db = new SqlDb();
SQLiteConnection sqLiteConnection = db.Connect();
SQLiteCommand sqLiteCommand = sqLiteConnection.CreateCommand();
try
{
modelData.Name = firstName.Text;
modelData.Age = Convert.ToInt32(DisplayAge.Text);
modelData.LastName = LastName.Text;
sqLiteCommand.CommandText =
$"insert into DANE values('{modelData.Name}', '{modelData.LastName}', '{modelData.Age}')";
db.Disconnect();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
我也将数据库选项设置为:
I also have the options of database set to:
Build Action: Content
Copy to output directory: Copy always
推荐答案
您忘记执行此命令:
sqLiteCommand.ExecuteNonQuery();
请参考文档:ExecuteNonQuery
这篇关于无法将记录插入到数据库 C# SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文