2个回答
展开全部
给你写个最简单的例子比如说页面上有三个TextBox控件!
//连接字符串
string ConString = ConfigurationManager.ConnectionStrings["DemoConnectionString"].ConnectionString.ToString();
protected void Page_Load(object sender, EventArgs e)
{
string sql = "select username,password,sex from TestDB wher id=1";//sql语句
SqlConnection con = new SqlConnection(ConString);
con.Open();//打开连接
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox1.Text = dr["username"].ToString();//texbox1的值
TextBox2.Text= dr["password"].ToString();//texbox2的值
TextBox3.Text= dr["sex"].ToString();//texbox3的值
}
con.Close();//关闭连接
}
注意:不管你是想获取那个字段的值,sql语句总重要!
//连接字符串
string ConString = ConfigurationManager.ConnectionStrings["DemoConnectionString"].ConnectionString.ToString();
protected void Page_Load(object sender, EventArgs e)
{
string sql = "select username,password,sex from TestDB wher id=1";//sql语句
SqlConnection con = new SqlConnection(ConString);
con.Open();//打开连接
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox1.Text = dr["username"].ToString();//texbox1的值
TextBox2.Text= dr["password"].ToString();//texbox2的值
TextBox3.Text= dr["sex"].ToString();//texbox3的值
}
con.Close();//关闭连接
}
注意:不管你是想获取那个字段的值,sql语句总重要!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询