how to get a return value of a sql statement in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • feariel
    New Member
    • Feb 2008
    • 1

    how to get a return value of a sql statement in c#

    hi,
    i am a beginner in programming and new to c# as well.
    i am using c# to access an access database that has one table named people.
    the table has three columns: firstName lastName TZ.
    i successfully wrote a method that inserts a new row to the table by using
    OleDb objects.

    Now i am trying to write a method that locates the appropriate TZ value
    upon receiving as input a firstName and lastName.

    i wrote this:

    string constring = @"Provider=Micr osoft.Jet.OLEDB .4.0; User Id=; Password=; Data Source=Db1.mdb" ;

    string sqls = "SELECT TZ FROM People WHERE firstName ='" + fname.Text + "' AND lastName ='" + lname.Text + "' ";

    where fname.Text amd lastName.Text are text fields on my form which contain the use input on the name he wants to find the TZ for.

    then i wrote:

    Myconnection = new OleDbConnection (constring);
    Myconnection.Op en();
    OleDbCommand cmd = new OleDbCommand(sq ls, Myconnection);
    OleDbDataReader dataread = cmd.ExecuteRead er();

    and here i got stuck since i wasnt able to find any method that simply submits
    my sql query and returns the value of that query.
    the connection to the database works perfectly fine.

    all help will be appreciated :)

    thank you
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Your dataread object now contains all the results of the query.
    You would do something like:
    Code:
    while (dataread.Read())
    {
    //dataread is now at a record
    //retrieve values for the given record with the .GetValue() functions
    }

    Although I much more recomend using an OleDbDataAdapte r and having it .Fill() a DataTable for you.

    Comment

    Working...