I don't know why I can't read on sqlreader

Asked 2 years ago, Updated 2 years ago, 51 views

I'm implementing the function of getting the value of row 3 when I press the button, but I can't read the db.

private void btnSearch_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=YONGCHEOL;Initial Catalog=ADO;Integrated Security=True");```

           conn.Open();

            string suzy = "SELECT * FROM rankTBL where NAME='" + txtName+ "' AND CLASS='" + ClassNum+ "'";
            SqlCommand cmd = new SqlCommand(suzy, conn);
            SqlDataReader suzyread = cmd.ExecuteReader();

            if (suzyread.Read())
            {
                kor = suzyread.GetInt16(3);
                lbEng.Text = kor.ToString();
            }
            else
            {
                MessageBox.Show ("Can't Read");
            }

            suzyread.Close();
            conn.Close();
        }

This is when the if statement is cleared and executed.

c#

2022-09-22 21:25

1 Answers

There seems to be no problem with grammar.

If you have any other problems, I can't clearly understand what the problem is based on your questions.

There are things you need to check in order to answer correctly.

I think you should check if the connection is made.

If the connection is successful, you should check to see if the query is feasible and if the results are as good as expected.

If the above things are executed well, I think we should check the results of the query execution.

And the part that you captured and uploaded...

The original code executes suzyread.Read() in the if statement.

This is a problem caused by not reading the data because I deleted the if statement.


2022-09-22 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.