类代码:
7
7
-1
aspx.cs代码:
using System.Data.SqlClient;
namespace WebApplication5
{
public class Class1
{
public int ID;
public string nem;
public int salar;
public void ReadIMP(int id )
{
SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=emp;Integrated Security=True");
string Query = "select * from employee where Id = '" + id + "' ";
SqlCommand cmd = new SqlCommand(Query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ID = (int)reader["Id"];
nem = (string)reader["name"];
salar = (int)reader["salary"];
}
reader.Close();
conn.Close();
}
}
}
aspx代码:
protected void Read_IMP(object sender, EventArgs e)
{
try
{
Class1 class1 = new Class1();
class1.ReadIMP(Convert.ToInt16(TextBox1.Text));
}
catch (Exception ex)
{
LabelEX.Text = ex.Message;
}
}
............................................... .................................... 我正在使用类中的方法从数据库中检索数据并在aspx.cs页面中调用该方法 我要填充以下属性:ID,nem,salar到Label1,Label2,Label3中我可以做到这一点
答案 0 :(得分:0)
从代码中,您已经在字段中填写了数据,然后将这些数据直接填充到aspx
控件中
Class1 class1 = new Class1();
class1.ReadIMP(Convert.ToInt16(TextBox1.Text));
Label1.Text = (string)class1.ID;
Label2.Text = class1.nem;
Label3.Text = (string)class1.salar;