如何使用选择查询填充标签? [英] how to fill labels with a select query?

查看:77
本文介绍了如何使用选择查询填充标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

如何从选择的查询中填写标签或文本框?

for examle my select reture the four fields(name,brand,model,price)

i希望在asp.net中做到这一点

感谢所有

hi.
how can i fill labels or textboxes from a select query?
for examle my select reture the four fields ( name , brand , model , price)
i want to do that in asp.net
thanks all

推荐答案

而不是自己编写数据。我建议您使用DataGrid控件。只需将查询结果传递给DataGrid,它就会自行处理。 DataGrid使用您传递的数据,并迭代数据并填充表。它自己创建列和行。



https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid(v = vs.110).aspx [ ^ ]

https://msdn.microsoft.com/en- us / library / aa479316.aspx [ ^ ]



如果您不使用该控件,则必须手动实施该算法。要使用列名生成表并写入标题行,并为每个记录循环其他行并填充表。这将花费很多时间。
Instead of writing the data yourself. I would recommend that you use DataGrid controls. Just pass the result of the query to the DataGrid and it would take care of it itself. DataGrid uses the data you pass, and iterates over the data and fills the table. It creates the column and rows itself.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/aa479316.aspx[^]

If you do not use the control, you would have to manually implement the algorithm. To generate a table and write header rows using the column names and for each record loop the other rows and fill up the table. It would take a lot of time.


如果您从数据库中搜索并显示单个项目,您可以执行以下操作

if you searching and display single item from database you can do as below
using (var connection = new SqlConnection("connection string"))
{
    connection.Open();
    using (var cmd = new SqlCommand("SELECT name , brand , model , price  FROM Product WHERE ProductID=@MYVALUE", connection))
    {
        cmd.Parameters.AddWithValue("@MYVALUE", txtID.Text);
        SqlDataReader re = cmd.ExecuteReader();

        if (re.Read())
        {
            txtName.Text = (string)re["name"]; 
            txtBrand.Text = (string)re["brand"];
            txtModel.Text = (string)re["model"];
            txtPrice.Text = (string)re["price"];
        }
        else
        {
            MessageBox.Show("Please enter a valid product id");
        }
    }
} 


基于用户输入的
,sql命令将获取数据,它将在UI上显示记录详细信息标签。



如果您需要显示多条记录,最好使用DataGridView或ListView等控件。


based on user input, sql command will fetch the data and it will display the record details on UI labels.

if you need to show multiple records, you better use controls like DataGridView or ListView.


这篇关于如何使用选择查询填充标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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