我正在通过以下方式从数据库中获取数据:
public static DataSet read_db(string sql)
{
DataSet ds = new DataSet();
try
{
SQLiteConnection m_dbConnection;
m_dbConnection = new SQLiteConnection("Data Source=movies.db;Version=3;");
m_dbConnection.Open();
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
//reader = command.ExecuteReader();
using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(command))
{
adapter.Fill(ds);
}
m_dbConnection.Close();
return ds;
}
catch (SQLiteException ex)
{
string code = ex.Message.ToString();
MessageBox.Show("Error connection to database: " + code);
return ds;
}
}
现在,我试图遍历该数据集,并从数据库中获取值,使用的是来自sQLite的常规数据读取器,但这无法正常工作,因为它会保持连接打开直到我完成读取为止,这意味着只要我的应用程序是打开的。
所以现在我必须将所有数据读取器更改为使用数据集的方式,这是我以前的数据读取器方式:
SQLiteDataReader reader = database.read_db("select * from proxies");
while (reader.Read())
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(proxytable);
row.Cells[0].Value = reader["id"];
row.Cells[1].Value = reader["proxy"];
row.Cells[2].Value = reader["port"];
row.Cells[3].Value = reader["username"];
row.Cells[4].Value = reader["password"];
if (reader["dead"].ToString() == "0")
{
status = "ok";
}
else if(reader["dead"].ToString() == "2")
{
status = "not tested";
}
else
{
status = "ok";
}
row.Cells[5].Value = status;
this.proxytable.Rows.Add(row);
}
如何更改此设置以与数据集一起使用?如果可能的话,因为我一直在使用此功能,所以只需进行很少的更改即可。
答案 0 :(得分:0)
尝试这样。...
DataSet ds= database.read_db("select * from proxies");
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow drw in ds.Tables[0].Rows)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(proxytable);
row.Cells[0].Value = drw ["id"];
row.Cells[1].Value = drw ["proxy"];
row.Cells[2].Value = drw ["port"];
row.Cells[3].Value = drw ["username"];
row.Cells[4].Value = drw ["password"];
}
}