如何使用 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 数据库中获取值到文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)