c#reader.read循环不起作用

时间:2018-05-29 10:46:37

标签: c#

我想从mysql数据库中检索一些数据并填充一些文本框。问题是编译器没有进入while(rdr.Read())loop.i检查了我的查询但是它是正确的。还有传递给构造函数的变量是正确的。 这是我的代码

namespace LoanApp
{
public partial class TablePopUp : Form
{
    string id;

    public TablePopUp(string var)
    {
        id = var;
        InitializeComponent();

        try
        {
            ConnectDB connection = new ConnectDB();
            MySqlConnection con = connection.setUpConnection();

            con.Open();

            string query = "SELECT * FROM debtors WHERE NIC = 'id'";
            MySqlCommand cmd3 = new MySqlCommand(query, con);
            MySqlDataReader rdr = cmd3.ExecuteReader();
            textBox1.Text = var.ToString();
            while (rdr.Read())
            {
                txtBoxSearch.Text = rdr.GetValue(1).ToString();
                textBox2.Text = rdr.GetValue(5).ToString();
                textBox3.Text = rdr.GetValue(4).ToString();

            }

        rdr.Close();
        con.Close();
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }
    }


   }

}

enter image description here

1 个答案:

答案 0 :(得分:1)

我想你想用实际的WHERE NIC = 'id'替换id,所以使用sql参数

string query = "SELECT * FROM debtors WHERE NIC = @id";
cmd3.Parameters.AddWithValue("@id", id);