[英]Firebird ADO.NET connection does not dispose file properly after disposing the connection
此方法必須訪問數據庫文件並執行簡單查詢:
private static DataTable QueryStuff(string connectionString, string[] types)
{
string queryTypes = "('" + string.Join("', '", types) + "')";
DataTable dtResults = new DataTable();
try
{
using (FbConnection myConnection1 = new FbConnection(connectionString))
{
myConnection1.Open();
FbTransaction fbTransaction = myConnection1.BeginTransaction();
FbCommand fbCommand = new FbCommand();
fbCommand = new FbCommand()
{
CommandText = "SELECT * FROM TABLE WHERE TYPE IN " + queryTypes + ";",
CommandType = CommandType.Text,
Connection = myConnection1,
Transaction = fbTransaction
};
FbDataReader dr = fbCommand.ExecuteReader();
dtResults.Load(dr);
dr.Close();
fbTransaction.Commit();
}
}
catch (Exception ex)
{
MessageBox.Show("Erro: " + ex.Message);
}
return dtResults;
}
之后,程序必須刪除數據庫文件,如下所示:
if (File.Exists(filePath))
File.Delete(filePath);
但它不能,因為該文件正在被“另一個進程”使用。
程序本身已經創建了該文件,我確信該文件沒有在任何其他進程中使用。
有沒有更好的方法來處理這個連接?
這可能與池選項有關:
“數據庫連接池是一種用於保持數據庫連接打開的方法,以便其他人可以重用它們。
通常,打開數據庫連接是一項昂貴的操作,尤其是在遠程數據庫的情況下。 您必須打開網絡會話、進行身份驗證、檢查授權等等。 池化使連接保持活動狀態,以便在稍后請求連接時,使用其中一個活動連接,而不是必須創建另一個連接。”
(來源: https://stackoverflow.com/a/4041136/14131713 )
要禁用此功能,您必須編輯連接字符串,將“Pooling = false”添加到其中:
$"User={user};" +
$"Password={password};" +
$"Database={path};" +
"DataSource=localhost;" +
**"Pooling=false";**
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.