简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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