Wednesday, February 26, 2014

Reading a Record/Row from SQLServer Database Using C#

First, Set up your connectionString and use the following code snippet in a method.

using (SqlConnection con = new SqlConnection(connectionString))
{

    con.Open();

    SqlCommand cmd = new SqlCommand("SELECT userid, username, email, city FROM USERS where username=@username and password=@password", con);
    cmd.Paramters.AddWithValue("@username", username);
    cmd.Parameters.AddWithValue("@password", password);
    cmd.CommandType = CommandType.Text;

    UserInfo info = new UserInfo();

    using (SqlDataReader rdr = cmd.ExecuteReader())
    {

        if (rdr.HasRows)
        {
            rdr.Read(); // get the first row

            info.UserID = rdr.GetInt32(0);
            info.UserName = rdr.GetString(1);
            info.Email = rdr.GetString(2);
            info.City = rdr.GetString(3);
        }
    }
}
Acknowledgement to: Tim of StackOverFlow.com

No comments:

Post a Comment