[英]c# sqlite database locked when inserting
我在C#中使用SQLite
我在可以正常使用的win窗體中有一個插入查詢,但在另一個不可行的win窗體上。 當我讀到這個問題時,它說這可能是因為某個地方有一個打開的連接,但我已經檢查了所有連接,甚至使用了SQLiteConnection.ClearAllPools();
每次關閉后。 現在第一個winform只有1個打開/關閉,另一個有3個。
我已嘗試使用SQLite瀏覽器查詢該查詢,以確保它不是該查詢的問題,並且可以成功運行。 現在,當我調試時,此行執行cmd.ExecuteNonQuery();
時會發生問題cmd.ExecuteNonQuery();
當然可以執行查詢(插入....)。 所以我嘗試更改表主鍵的類型(從varchar更改為integer)。 而且我仍然有問題。 以下是它的總結。
myconnection = new SQLiteConnection(connectionString);
myconnection.Open();
//select stuff here
//verifications here
//insert inside verification
//finally { myconnection.close(); }
您是否嘗試將連接包裝在using語句中?
using(var myconnection = new SQLiteConnection(connectionString))
{
myconnection.Open();
//Your code here
}
無論執行路徑如何,這都將調用連接的Dispose方法,這可能不僅僅是關閉連接。
可能是你也應該嘗試這個
using (var con = new SQLiteConnection { ConnectionString = "connectionstring" })
{
using(var cmd = new SQLiteCommand { Connection = con })
{
// check state connection if open then close, else close proceed
if(con.State == ConnectionState.Open)
con.Close();
//then
try
{
// try connection to Open
con.Open();
}
catch
{
//catch if found error, message : 'Invalid Connection string'
}
........ // insert query here
} // Close Command
} // Close Connection
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.