如何使用 C# 从 SQL 数据库中获取值到文本框中?

How do I get values from a SQL database into textboxes using C#?(如何使用 C# 从 SQL 数据库中获取值到文本框中?)

本文介绍了如何使用 C# 从 SQL 数据库中获取值到文本框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个预订管理系统,但在尝试从 SQL 数据库获取数据并将其插入到我的应用程序的一组文本框中时遇到问题.

I'm creating a booking management system and I am having problems trying to get data from a SQL database and insert into a group of textboxes of my application.

我想在 DataGridView 中单击按钮时显示客户详细信息,但是当我单击按钮时,应用程序会引发异常并显示以下错误消息;

I want to show the customer details, when a button is clicked, in a DataGridView, but when I click the button, the application throws an exception with the following error message;

不存在数据时尝试读取无效.

Invalid attempt to read when no data is present.

我附上了我想要查看客户详细信息的屏幕的屏幕截图,以及按钮的代码,最终将在相应的文本框中显示客户详细信息.任何帮助将不胜感激!

I have attached a screenshot of the screen where I want to view customer details, and the code for the button, which will eventually show customer details in the respective textboxes. Any help would be greatly appreciated!

    SqlConnection sc = new SqlConnection("Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True");
        SqlCommand com = new SqlCommand();
        com.Connection = sc;
        sc.Open();
        SqlDataReader read = (null);
        com.CommandText = ("select * from Pending_Tasks");
        read = com.ExecuteReader();
        CustID.Text = (read["Customer_ID"].ToString());
        CustName.Text = (read["Customer_Name"].ToString());
        Add1.Text = (read["Address_1"].ToString());
        Add2.Text = (read["Address_2"].ToString());
        PostBox.Text = (read["Postcode"].ToString());
        PassBox.Text = (read["Password"].ToString());
        DatBox.Text = (read["Data_Important"].ToString());
        LanNumb.Text = (read["Landline"].ToString());
        MobNumber.Text = (read["Mobile"].ToString());
        FaultRep.Text = (read["Fault_Report"].ToString());
        sc.Close();

推荐答案

您的代码中缺少行 reader.Read().你应该添加它.它是实际从数据库中读取数据的函数:

The line reader.Read() is missing in your code. You should add it. It is the function which actually reads data from the database:

string conString = "Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True";
SqlConnection con = new SqlConnection(conString);

string selectSql = "select * from Pending_Tasks";
SqlCommand com = new SqlCommand(selectSql, con);

try
{
    con.Open();

    using (SqlDataReader read = cmd.ExecuteReader())
    {
        while(reader.Read())
        {
            CustID.Text = (read["Customer_ID"].ToString());
            CustName.Text = (read["Customer_Name"].ToString());
            Add1.Text = (read["Address_1"].ToString());
            Add2.Text = (read["Address_2"].ToString());
            PostBox.Text = (read["Postcode"].ToString());
            PassBox.Text = (read["Password"].ToString());
            DatBox.Text = (read["Data_Important"].ToString());
            LanNumb.Text = (read["Landline"].ToString());
            MobNumber.Text = (read["Mobile"].ToString());
            FaultRep.Text = (read["Fault_Report"].ToString());
        }
    }
}
finally
{
    con.Close();
}

假设您要将最后一条记录写入文本框,则此代码有效.如果您想应用不同的场景,例如从数据库中读取所有记录并在单击 Next 按钮时更改 texboxes 中的数据,您应该创建并使用自己的模型,或者您可以将数据存储在 DataTable 中,如果您愿意,以后可以参考它们.

EDIT : This code works supposing you want to write the last record to your textboxes. If you want to apply a different scenario, like for example to read all the records from database and to change data in the texboxes when you click the Next button, you should create and use your own Model, or you can store data in the DataTable and refer to them later if you wish.

这篇关于如何使用 C# 从 SQL 数据库中获取值到文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 C# 从 SQL 数据库中获取值到文本框中?

基础教程推荐