简体   繁体   中英

using sqldatareader with where clause in query

I need to read a specific value from the table, to use it in my field.
But when I insert the where clause in query, it gets me null value, nothing more.

My code is:

string query_select_id = "SELECT * from table1 WHERE column1=@value";

SqlConnection con = new SqlConnection(ConString);
try
{
    con.Open();
    SqlCommand createCommand = new SqlCommand(query_select_id, con);
    createCommand .Parameters.Add("@value", SqlDbType.Int).Value  =Convert.ToInt32(textbox.Text);

    SqlDataReader dr = createCommand.ExecuteReader();
    while (dr.Read())
    {
        intvalue= dr.GetInt32(12); 
        // 12 is the index of a column in table of the value I wanna get
    }
    con.Close();

    textboxvalue.Text = intvalue.ToString();

}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

It is very bad idea to use SELECT * instead of SELECT column1 and dr.GetInt32(12) (constant index of value in the result). Try SELECT column1 FROM table1 WHERE column1=@value and use ExecuteScalar() to get the only result instead of SqlDataReader and while loop.

If you need only one value, you should use ExecuteScalar method. I use TOP 1 and column name to ensure that only one value is returned from select statement.

string query_select_id = "SELECT TOP 1 COLUMN12 from table1 WHERE column1=@value";
SqlConnection con = new SqlConnection(ConString);
int intvalue;
try
{
    con.Open();
    SqlCommand createCommand = new SqlCommand(query_select_id, con);
    createCommand.Parameters.Add("@value", SqlDbType.Int).Value  =Convert.ToInt32(textbox.Text);
    object result = cmd.ExecuteScalar();
    if(result == null)
    {
        textboxvalue.Text = "Result is NULL";
    }
    else
    {

        intvalue = (int) result; 
        textboxvalue.Text = intvalue.ToString();
    }
    con.Close();


}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Try this:

Change createCommand.Parameters.Add into createCommand.Parameters.AddWithValue

    string query_select_id = "SELECT * from table1 WHERE column1=@value";

    SqlConnection con = new SqlConnection(ConString);

    try
    {
        con.Open();
        SqlCommand createCommand = new SqlCommand(query_select_id, con);
        //createCommand .Parameters.AddWithValue("@value", SqlDbType.Int).Value=Convert.ToInt32(textbox.Text);
        createCommand .Parameter.AddWitheValue("@value",textbox.Text);
        SqlDataReader dr = createCommand.ExecuteReader();
        while (dr.Read())
        {
            intvalue= dr.GetInt32(12); 
        // 12 is the index of a column in table of the value I wanna get

        }
        con.Close();

        textboxvalue.Text = intvalue.ToString();

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

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