放在已个按钮事件下面,先说,如果有错,你要调试哈,要引用命名控件才可以哦
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();
}
}
搞定