C#如何用lable控件,将数据库中查询到的值显示出来?求完整的代码
如图,假设查询sno=1时的sname属性的值假设数据库连接字符串为IntegratedSecurity=SSPI;PersistSecurityInfo=False;I...
如图,假设查询sno=1时的sname属性的值
假设数据库连接字符串为
Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=myproject;Data Source=. 展开
假设数据库连接字符串为
Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=myproject;Data Source=. 展开
展开全部
放在已个按钮事件下面,先说,如果有错,你要调试哈,要引用命名控件才可以哦
string ConnectionString=“Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=myproject;Data Source=.”
string sql="select * from 你的表名";
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
try
{
connection.Open();
SqlCommand command = new SqlCommand(stsql,connection);
SqlDataReader SDR = command.ExecuteReader();
if (SDR.Read())//只读第一条数据
{
lable1.Text=SDR[0].ToString();//SNO
lable2.Text=SDR[1].ToString();//SNAME
lable3.Text=SDR[2].ToString();//ssex
}
SDR.Close();
SDR.Dispose();
connection.Close();
connection.Dispose();
}
catch (Exception ex)
{
connection.Close();
connection.Dispose();
}
}
搞定
string ConnectionString=“Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=myproject;Data Source=.”
string sql="select * from 你的表名";
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
try
{
connection.Open();
SqlCommand command = new SqlCommand(stsql,connection);
SqlDataReader SDR = command.ExecuteReader();
if (SDR.Read())//只读第一条数据
{
lable1.Text=SDR[0].ToString();//SNO
lable2.Text=SDR[1].ToString();//SNAME
lable3.Text=SDR[2].ToString();//ssex
}
SDR.Close();
SDR.Dispose();
connection.Close();
connection.Dispose();
}
catch (Exception ex)
{
connection.Close();
connection.Dispose();
}
}
搞定
追问
谢谢你,我已经搞定了。
追答
不用!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询