C# SQLiteでデータ取得 SqliteDataReaderを使う

VisualStudio

本ページは広告が含まれています。気になる広告をクリック頂けますと、サーバ運営費になります(^^

SELECTして値を取得したい

VisualStudio 2017 C#の環境で

Microsoft.Data.Sqlite

を利用しています。

SqliteDataReaderを使う

サンプルです。

tm_productテーブルにある、productidを取得し、取得した分だけメッセージボックスを表示させるものです。

using (SqliteConnection con = new SqliteConnection(connectionString))
{
    con.Open();
    try
    {
        using (SqliteCommand command = con.CreateCommand())
        {
            command.CommandText = "SELECT productid FROM tm_product";
            SqliteDataReader sdr = command.ExecuteReader();

            while (sdr.Read() == true)
            {
                MessageBox.Show(sdr["productid"].ToString());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        con.Close();
    }
}

 

connectionStringは以下参照 パスワード使うなら Microsoft.Data.Sqlite で

パスワード使わないなら System.Data.SQLite で

タイトルとURLをコピーしました