我使用的提取方法對這個代碼,因爲有這個代碼的倍數,這裏是它變成了:C# - 通過不同類型作爲一個參數
private void InsertStatement(string table, string table2, TestURLGUI4.Form1 form, SQLiteConnection sql_con, ref int dbID, ref int dbID2, Chrome chrome, int max)
{
try
{
List<int> dbIDs = new List<int>();
using (SQLiteTransaction mytransaction = sql_con.BeginTransaction())
{
using (SQLiteCommand mycommand = new SQLiteCommand(sql_con))
{
mycommand.CommandText = "insert or ignore into " + table + " (id, url, title, visit_count, frecency, last_visit_date) values (@dbID,@url,@title,@visit,@frecency,@time)";
for (var count2 = 0; count2 < chrome.URLs.Count; count2++)
{
URL u = chrome.URLs[count2];
mycommand.Parameters.Add(new SQLiteParameter("@dbID", dbID));
mycommand.Parameters.Add(new SQLiteParameter("@url", u.url));
mycommand.Parameters.Add(new SQLiteParameter("@title", u.title));
mycommand.Parameters.Add(new SQLiteParameter("@visit", u.frequency));
mycommand.Parameters.Add(new SQLiteParameter("@time", ToPRTime(u.visited)));
mycommand.Parameters.Add(new SQLiteParameter("@frecency", ToFrecency(u.frequency)));
mycommand.ExecuteNonQuery();
dbIDs.Add(dbID);
dbID++;
form.label1.Text = count2 + "/" + max;
Application.DoEvents();
}
}
mytransaction.Commit();
}
using (SQLiteTransaction mytransaction = sql_con.BeginTransaction())
{
using (SQLiteCommand mycommand = new SQLiteCommand(sql_con))
{
mycommand.CommandText = "insert or ignore into " + table2 + " (id, from_visit, place_id, visit_date, visit_type, session) values (@dbID2,2,@dbID,@time,1, 0)";
for (var count2 = 0; count2 < chrome.URLs.Count; count2++)
{
URL u = chrome.URLs[count2];
mycommand.Parameters.Add(new SQLiteParameter("@dbID2", dbID2));
mycommand.Parameters.Add(new SQLiteParameter("@dbID", dbIDs[count2]));
mycommand.Parameters.Add(new SQLiteParameter("@time", ToPRTime(u.visited)));
mycommand.ExecuteNonQuery();
dbID2++;
form.label1.Text = count2 + "/" + max;
Application.DoEvents();
}
}
mytransaction.Commit();
}
}
catch
{
throw;
}
}
唯一的問題是,而不是瀏覽器類型,參數,我已經創建了不同類別的多個實例,我需要通過每一個,例如,我有
IE ie = new IE();
Firefox firefox = new Firefox();
等。現在,我該怎麼修改我的參數,這樣,而不是瀏覽器,我可以通過Chrome,Firefox,IE等全部在同一個參數中,一次一個?
你問題的最後一句話讓我感到困惑 - 你希望能夠將任何瀏覽器作爲單個參數傳遞,或者將所有瀏覽器作爲單個參數傳遞? – Daniel 2012-08-12 22:18:01
出於興趣,做這些類中的任何一個從一個公共基類繼承嗎?因爲如果是這樣,前一種情況可以通過多態代碼輕鬆解決。 – Daniel 2012-08-12 22:18:54
它們中的任何一個參數都是一次一個參數。抱歉。 – 2012-08-12 22:22:02