简体   繁体   English

从sqlitereader c#遍历数据集

[英]loop through dataset from sqlitereader c#

I am getting data from a database via this: 我通过这个从数据库中获取数据:

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;
        }
    }

Now I am trying to loop through this dataset, and get the values from the database, was using a regular datareader from sQLite, but that wont work because it keeps the connection open until I am done reading, which means as long as my application is open. 现在,我试图遍历该数据集,并从数据库中获取值,使用的是来自sQLite的常规数据读取器,但这无法正常工作,因为它将保持连接打开,直到我完成读取为止,这意味着只要我的应用程序处于打开。

So now I have to change all my datareaders to a way with the dataset, this was my previous datareader way: 所以现在我必须将所有数据读取器更改为使用数据集的方式,这是我以前的数据读取器方式:

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);
            }

How can I change this to work with the dataset? 如何更改它以与数据集一起使用? If possible with as little change needed because I have been using this alot. 如果可能的话,只需进行很少的更改,因为我一直在使用此功能。

try Like this.... 尝试这样...

        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"];
        }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM