gpt4 book ai didi

c# - UWP 将 sqlite 插入查询插入 SQL 事务

转载 作者:行者123 更新时间:2023-12-02 03:15:20 24 4
gpt4 key购买 nike

我正在尝试将一些 csv 样式 file.txt 内容插入本地 SQLite 数据库,我处理文件读取、内容处理并获取一些字符串:

INSERT OR REPLACE INTO mytab (id,name,adress) VALUES(1,john,adress1)
INSERT OR REPLACE INTO mytab (id,name,adress) VALUES(2,marry,adress2)
INSERT OR REPLACE INTO mytab (id,name,adress) VALUES(3,lama,ruadress3)
//...

现在我想打开一个 sqlite 事务,执行所有这些插入操作,然后关闭事务。我正在使用 SQLite.Net-PCLSQLite for Universal App Platform,我该怎么做?

我尝试打开这样的连接:

var sqlpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Mydb.sqlite");
SQLiteConnection conn = new SQLiteConnection(new SQLitePlatformWinRT(), sqlpath);

//I'am trying to open transaction - but get ERROR -> SQLiteCommand does not contain contstructor that takes 2 arguments
SQLiteCommand cmd = new SQLiteCommand("BEGIN", conn);

最佳答案

SQLite.Net-PCL 中的 SQLiteConnection 类具有 BeginTransactionCommitRollback 方法,因此您可以在 BeginTransaction 和 Commit 之间包装命令:

// Open connection
SQLiteConnection conn = new SQLiteConnection(new SQLitePlatformWinRT(), sqlpath);
try {
// Start transaction
conn.BeginTransaction();
try {
// Execute commands...
// Commit transaction
conn.Commit();
}
catch (Exception) {
// Rollback transaction
conn.Rollback();
}
}
finally {
// Close connection
conn.Close();
}

关于c# - UWP 将 sqlite 插入查询插入 SQL 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434788/

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