我如何从一个SQL数据库值到使用C#文本框? [英] How do I get values from a SQL database into textboxes using C#?

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

问题描述

我创建一个预约管理系统,我有问题,试图从一个SQL数据库获取数据并插入到一组我的应用程序的文本框。

我想告诉客户的详细信息,单击时按钮,在一个DataGridView,但是当我点击该按钮,应用程序将引发异常,并出现以下错误信息;

  当没有数据是present

无效的尝试读取。

我附上一个截图,我要查看客户详细信息的屏幕,和$ C $下按钮,它最终将显示客户详细信息在各自的文本框。任何帮助将大大AP preciated!

 的SqlConnection SC =新的SqlConnection(数据源=本地主机;初始目录= LoginScreen;集成安全性=真);
        SqlCommand的COM =新的SqlCommand();
        com.Connection = SC;
        sc.Open();
        SqlDataReader的阅读=(空);
        com.CommandText =(SELECT * FROM Pending_Tasks);
        读= com.ExecuteReader();
        CustID.Text =(读[在Customer_ID]的ToString());
        CustName.Text =(读[CUSTOMER_NAME]的ToString());
        Add1.Text =(读[Address_1]的ToString());
        Add2.Text =(读[Address_2]的ToString());
        PostBox.Text =(读[邮报code]的ToString());
        PassBox.Text =(读[密码]的ToString());
        DatBox.Text =(读[Data_Important]的ToString());
        LanNumb.Text =(读[固定电话]的ToString());
        MobNumber.Text =(读作[移动]的ToString());
        FaultRep.Text =(读[Fault_Report]的ToString());
        sc.Close();
 

解决方案

reader.Read()在您的code失踪。你应该添加它。它是实际从数据库中读取数据的功能:

 字符串conString =数据源=本地主机;初始目录= LoginScreen;集成安全性=真;
SqlConnection的CON =新的SqlConnection(conString);

字符串selectSql =选择Pending_Tasks *;
SqlCommand的COM =新的SqlCommand(selectSql,CON);

尝试
{
    con.Open();

    使用(SqlDataReader的阅读= cmd.ExecuteReader())
    {
        而(reader.Read())
        {
            CustID.Text =(读[在Customer_ID]的ToString());
            CustName.Text =(读[CUSTOMER_NAME]的ToString());
            Add1.Text =(读[Address_1]的ToString());
            Add2.Text =(读[Address_2]的ToString());
            PostBox.Text =(读[邮报code]的ToString());
            PassBox.Text =(读[密码]的ToString());
            DatBox.Text =(读[Data_Important]的ToString());
            LanNumb.Text =(读[固定电话]的ToString());
            MobNumber.Text =(读作[移动]的ToString());
            FaultRep.Text =(读[Fault_Report]的ToString());
        }
    }
}
最后
{
    con.Close();
}
 

编辑::此code ++工程,假设你想要写的最后一条记录的文本框。如果你想申请一个不同的场景,例如像读取数据库中的所有记录和修改数据的texboxes当您单击下一页按钮,您应该创建并使用自己的型号,或者您也可以将数据存储在数据表中,后来参考,如果您愿意。

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.

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();

解决方案

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();
}

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆