调用数据库中某特定项的方法

此例的数据库为SqlServer。调用的代码如下:可以通过点击按钮来触发事件。在aspx页面中添加两个textbox控件和button控件。双击button按钮,在aspx.cs页面中

protected void Button1_Click(object sender, EventArgs e)

{

  string sql="select * from tbale1 where ...";//where条件根据需要自己写用来筛选行

  SqlConnection con= new SqlConnection("server=.;database=你调用数据库的名称;User id=账号;pwd=密码;");

  con.open();//此处为本机服务器

  SqlCommand cmd= new SqlCommand(sql,con);

  SqlDataReader dr= cmd.ExecuteReader();

  while(dr.Reader())

  {

     //下面是用容器盛放选中项,例如用textbox控件

     TextBox1.Text= dr["列名1"].ToString();

     TextBox2.Text= dr["列名2"].ToString();//联合前面的where选中的行就可以选中你所需要的数据了

  }

  con.Close();

}

调试运行程序之后就会在页面对应的textbox控件中看到你所需要的数据库中的某项了

原文地址:https://www.cnblogs.com/sushuai/p/2484984.html