in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Basic help - select query result...

Last post 03-09-2010 11:17 PM by Paul. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 03-08-2010 4:51 PM

    Basic help - select query result...

    Hi, I'm a newbiz in sqlite (and in C#)... and I try to find an answer on google and this fourm before posting.

    Since some months (when I have time) I try to learn C#. I give myself a goal to learn the language : write a text gui game, I was thinking that a database would be too heavy to manipulate and I start using csv files... I'm feed up to parse files and to retrieve fields and to update it, etc... so I search a bit and find sqlite, it could solve many of my repetitive operations (most actions is reading data and update data).

    In order to learn the way it should I progress by step, first step for me is textbox filling, and I'm ever stuck...

    My first and simple goal is to fill the textbox1 text propertie with the result of a query...

    I have a one line table called MAIN, in a database named MRIS, and I want to grab the value in a column named YEAR.
    here is my "bad" really not working code...
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SQLite;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    SQLiteConnection ObjConnection = new SQLiteConnection("Data Source=Data/MRIS;");
    SQLiteCommand ObjCommand = new SQLiteCommand("SELECT * FROM MAIN", ObjConnection);
    ObjCommand.CommandType = CommandType.Text;
    SQLiteDataAdapter ObjDataAdapter = new SQLiteDataAdapter(ObjCommand);
    DataSet dataSet = new DataSet();
    textBox1.Text = ??????????????????? ==> I don't know what to do or I the code above is right or too heavy...
    }
    }
    }


    If any of you have some time to write a new howto (another ideas... "fill a combox with select query result" ;-)).

    Thanks from Paris.
    Filed under: ,
  • 03-09-2010 11:17 PM In reply to

    • Paul
    • Top 25 Contributor
    • Joined on 09-12-2007
    • Boise
    • Posts 56

    Re: Basic help - select query result...

    Here is a way you can do this. I would ask these questions at stackoverflow.com, where there is more activity.

    using(SQLiteConnection conn = new SQLiteConnection(@"Data Source = Data\MRIS.sqlite"))
    {
        conn.Open();

        // Fill a combo box:
        SQLiteCommand cmd = new SQLiteCommand("select YEAR from MAIN", conn);
        SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd);
        DataTable tbl = new DataTable();
        adapter.Fill(tbl);
        comboBox1.DataSource = tbl;
        comboBox1.DisplayMember = "ItemName";
        adapter.Dispose();
        cmd.Dispose();

        // Fill a text box:
        SQLiteCommand cmd2 = new SQLiteCommand("select YEAR from MAIN where YEAR = 2010", conn);
        SQLiteDataReader rdr = cmd2.ExecuteReader();
        if(rdr.Read())
            textBox1.Text = rdr[0].ToString();
        rdr.Dispose();
        cmd2.Dispose();
    }

Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems