如何从标签中的数据库中检索数据 [英] How to retrieve data from database in label

查看:90
本文介绍了如何从标签中的数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中检索学生ID。这是我的代码,我试过但它没有显示任何内容。







谢谢.. 。



我尝试了什么:



protected void Button2_Click(object sender,EventArgs e)

{



SqlConnection con = new SqlConnection(Data Source = DESKTOP-Q69PRF4; Initial Catalog = new; Integrated安全=真);





if(con.State == ConnectionState.Open)

{

con.Close();

}

con.Open();

string str =select *来自StRecords,其中StID ='+ Session [login] +';

SqlCommand com = new SqlCommand(str,con);

SqlDataAdapter da = new SqlDataAdapter(com);

DataSet ds = new DataSet();

DataTable dt = new DataTable();

da.Fill( dt);

if(dt.Rows.Count> 0)

{

Label11.Text = dt.Rows [0] [StID]。ToString();



}





}

I want to retrieve student ID from database. Here is my code what i have tried but it is not displaying anything.



Thanks...

What I have tried:

protected void Button2_Click(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection("Data Source=DESKTOP-Q69PRF4;Initial Catalog=new;Integrated Security=True");


if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
string str = "select * from StRecords where StID='" + Session["login"] + "'";
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Label11.Text = dt.Rows[0]["StID"].ToString();

}


}

推荐答案

试试这个



Try this

 DataTable dt = new DataTable();
            try
            {
                con = da_con.DA_Connect();
                SqlCommand cmd = new SqlCommand("FetchShopSellerOnId", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter sqlDa = new SqlDataAdapter();
                sqlDa.SelectCommand = cmd;
                sqlDa.Fill(dt);
<pre> foreach (DataRow dRow in dt.Rows)
                {
                    if (dt.Rows.Count > 0)
                    {
           Label11.Tex   = dRow["ShopOfficeDetailsId"].ToString();

                  }
}









In Above Code





In Above Code

dRow["ShopOfficeDetailsId"].ToString();

ShopOfficeDetailsID用于存储过程,例如。选择ShopOfficeDetailsID ,,您可以使用您的ID名称

ShopOfficeDetailsID is used in stored procedure eg. Select ShopOfficeDetailsID ,, You can use your ID name


这篇关于如何从标签中的数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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