3
林清楚地意識到,我應該做這樣的SELECT查詢:С#與System.Data.SQLite - 關閉連接
System.Data.SQLite.SQLiteConnection scrsql_con = new System.Data.SQLite.SQLiteConnection("Data Source=db.db;Version=3;New=False;Compress=True;");
scrsql_con.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = "Select something FROM something";
cmd.Connection = scrsql_con;
SQLiteDataReader dr = cmd.ExecuteReader();
//reading stuff from datareader...
dr.Close();
scrsql_con.Close();
然而,也有很多在我的應用程序SELECT查詢的,所以我決定爲此做一個方法。 現在它看起來像以下:
public static SQLiteDataReader GenericSelect(String query)
{
System.Data.SQLite.SQLiteConnection scrsql_con = new System.Data.SQLite.SQLiteConnection("Data Source=SCRdb.db;Version=3;New=False;Compress=True;");
scrsql_con.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = query;
cmd.Connection = scrsql_con;
SQLiteDataReader dr = cmd.ExecuteReader();
return dr;
}
但它不是很好,因爲它留下scrsql_con hanging.I不能從GenericSelect方法內關閉它,因爲這意味着它總是返回空的DataReader或錯誤,我無法從外面關閉它。 任何建議,我應該怎麼辦GenericSelect妥善所以它使返回的DataReader?
我知道我可以使用的數據表,但除了性能,在很多地方使用這個方法,所以我將節省大量的時間,如果它不斷回來,他現在返回了什麼。
你GenericSelect方法是'static'所以創建和銷燬每次調用該方法時,您的連接。 – banging 2013-05-10 19:29:26
你爲什麼不返回一個DataTable? – banging 2013-05-10 19:30:21
因爲他需要改變每調用GenericSelect程序?因爲填充一個DataTable然後在這個循環上有效地將性能命中翻倍? – Steve 2013-05-10 19:37:16