简体   繁体   English

我想通过输入要加载的值的特定ID将数据库中的值加载到文本框中

[英]I want to load values from my database to the textboxes by typing in the particular ID of the values to load

I want to load the values from my database by typing the ID that I want to load, example is library, I want to load the values of Book Id #2 to the textbox the code works, but it only displays the last values in the database or the last row, it doesn't follow the book id that I typed 我想通过键入要加载的ID从数据库中加载值,例如库,我想将Book Id#2的值加载到代码有效的文本框中,但它仅显示代码中的最后一个值。数据库或最后一行,它不遵循我输入的书号

ex. 恩。 Book 1 = Harry Potter, JK Rowling. 书1 =哈利·波特(JK Rowling)。 Book 2 = Dictionary, Unknown. 书2 =字典,未知。 Book 3 = Science, Unknown. 书3 =科学,未知。

Load book id 1, but the results are Science, Unknown. 加载图书ID 1,但结果为“科学”,“未知”。

Here's the code 这是代码

try
{
    con.Open();

    adp = new SqlDataAdapter("SELECT COUNT(*) FROM Bookinfo WHERE BookNumber='" + textBox3.Text + "'", con);
    dt = new DataTable(); 
    adp.Fill(dt);

    if (dt.Rows[0][0].ToString() == "1")
    {
        cmd2 = new SqlCommand("SELECT * FROM Bookinfo WHERE BookNumber = '" + textBox3.Text + "';", con);

        using (SqlDataReader read = cmd.ExecuteReader())
        {
            while (read.Read())
            {
                textBox4.Text = (read["DateReceive"].ToString());
                textBox5.Text = (read["Class"].ToString());
                textBox6.Text = (read["Author"].ToString());
                textBox7.Text = (read["BookName"].ToString());
                textBox8.Text = (read["Edition"].ToString());
                textBox9.Text = (read["Volume"].ToString());
                textBox10.Text = (read["Pages"].ToString());
                textBox11.Text = (read["Source"].ToString());
                textBox12.Text = (read["Price"].ToString());
                textBox13.Text = (read["Publisher"].ToString());
                textBox14.Text = (read["Year"].ToString());
                textBox15.Text = (read["Remarks"].ToString());
            }
        }

        con.Close();

        MessageBox.Show("Book Loaded");
    }
    else
    {
        MessageBox.Show("Book doesn't exist");
        con.Close();
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
};

Thank you 谢谢

You are filling the text boxes from a SqlReader attached to a SqlCommand called cmd. 您正在从附加到名为cmd的SqlCommand的SqlReader中填充文本框。 However, the shown SqlCommand is cmd2. 但是,显示的SqlCommand是cmd2。 You are not showing what you think you are. 您没有展示自己的想法。

Also get rid of the while (read.Read), you are just setting the same boxes again and again. 也摆脱了while(read.Read),您一次又一次地设置了相同的框。

Another thing: When you use ADO directly, there is so much boilerplater code you have to write, I recommend using something like Dapper, which takes a lot of the tedious work away. 另一件事:当您直接使用ADO时,您必须编写很多样板程序代码,我建议使用Dapper之类的东西,它可以省去很多繁琐的工作。

And as BWA said: Never concatenate Sql. 正如BWA所说:永远不要连接Sql。 Use parameters to avoid Sql-injection. 使用参数来避免Sql注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM