如何从sqlserver读取数据并在标签中显示它们? [英] how to read data from sqlserver and show them in label?

查看:163
本文介绍了如何从sqlserver读取数据并在标签中显示它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



我创建了一个Web应用程序,有人将他的个人信息插入到数据库中。最后他想看到他插入的数据。所以我决定制作如下的打印页面:



FirstName =========== FirstNameResultLabel

LastName == ========= LastNameResultLabel

BirthDate =========== BirthDateResultLabel



infact i would喜欢从sqlserver读取数据并在这些标签中显示它们。所以这是我的代码:

Hello.

I have created a web application that some one insert his personal information into the database.at the end he wants to see his inserted data. so i decided to make a printpage like below :

FirstName =========== FirstNameResultLabel
LastName =========== LastNameResultLabel
BirthDate =========== BirthDateResultLabel

infact i would like to read data from sqlserver and show them in these labels. so here is my code:

    SqlCommand Command = DataProvider.GenerateCommand("[dbo].[Select_IranianGeneralInfoReport_SP]", CommandType.StoredProcedure);
    Command.Parameters.AddWithValue("@IdCode", GlobalVariables.IdCode);
    try
    {
        Command.Connection.Open();
        GlobalVariables.IdCode = int.Parse(Command.ExecuteScalar().ToString());
        SqlDataReader dr = DataProvider.ExecuteDataReader("[dbo].[Select_IranianGeneralInfoReport_SP]", CommandType.StoredProcedure);
        FamilyResultLabel.Text = dr["LastName"].ToString();
        NameResultLabel.Text = dr["FirstName"].ToString();
        dr.Read();

        dr.Close();
        Command.Connection.Close();
    }
    catch (Exception ex)
    {
        EventLogger.Log(ex.Message, LogType.Error);
    }
}





所有内容都在PageLoad中。

但是当我开始调试时,发生异常。



我该如何解决这个问题?或者更好地说我如何从数据库中读取数据并在上面的标签中显示它们?



everything is in the PageLoad.
but when i start to debug , and exception occures .

how can i solve this problem ? or better to say how can i read data from database and show them in those above labels?

推荐答案





更改这些代码行。

Hi,

change these code lines.
SqlCommand Command = DataProvider.GenerateCommand("[dbo].[Select_IranianGeneralInfoReport_SP]", CommandType.StoredProcedure);
            Command.Parameters.AddWithValue("@IdCode", GlobalVariables.IdCode);
try
{
    Command.Connection.Open();
    GlobalVariables.IdCode = int.Parse(Command.ExecuteScalar().ToString());
SqlDataReader dr = DataProvider.ExecuteDataReader("Select_IranianGeneralInfoReport_SP", CommandType.StoredProcedure);
FamilyResultLabel.Text = "";
NameResultLabel.Text = "";
while(dr.Read())
{
FamilyResultLabel.Text = dr["LastName"].ToString();
NameResultLabel.Text = dr["FirstName"].ToString();
}
dr.Close();
Command.Connection.Close(); 
}
catch (Exception ex)
{
    EventLogger.Log(ex.Message, LogType.Error);
}





dr.Read()应该先执行阅读价值观。并将其置于while循环中将避免零行上的异常。

请参阅此文章以获取更多信息。

SqlDataReader类 [ ^ ]

SqlDataReader类 [ ^ ]



希望它有所帮助。



dr.Read() should be excuted before reading the values. and putting that in while loop will avoid exception on zero rows.
refer this article for more information.
SqlDataReader Class[^]
SqlDataReader Class[^]

hope it helps.


您好,



您可以使用以下内容:



Hi,

You can use something like below:

DataSet ds = new DataSet();
DataTable dt = new DataTable();

da.SelectCommand = new SqlCommand(@"SELECT * FROM dbo.MyTable", connString);
da.Fill(ds, "dbo.MyTable");
dt = ds.Tables["dbo.MyTable"];

foreach (DataRow dr in dt.Rows)
{
    Label1.Text=dr["Column1"].ToString();
    Label2.Text=dr["Column1"].ToString();

} 





以上是一种方法,但是当你有一张大桌子时会出现性能问题在数据库中,因为每次都会查询表,这会给你的代码增加一些开销。在这种情况下,你可以立即将整个表拉入数据表并使用它。



希望它有所帮助。



问候

Anurag



The above is a method, but it will have performance issues when you have a large table in the database, coz the table will be queried everytime and it will add a overhead to your code.In that case you can pull the whole table in a datatable at once and use it.

Hope it helps.

Regards
Anurag


这篇关于如何从sqlserver读取数据并在标签中显示它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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