学习.net有所收获了,来记录一下

一个小例子,以下测试均通过有效

要实现在下拉菜单中读取用户名
在表格中显示用户ID,用户名……

先写一个DB类


namespace _127._0._0._1.include
{
 public class DataBaseDB
 {
  //public static String ConnectionString = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"];
  //此方法调用web.config中的连接方法

  public static SqlConnection createCon()
  {
     return new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
  }
 }
 }
 

下面是.cs文件



protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  //绑定DropDownList
   SqlConnection myConnection = DataBaseDB.createCon();
   myConnection.Open();
   String cmdText = "select * from Employees ";
   SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
   SqlDataReader recn = myCommand.ExecuteReader();
   this.DropDownList1.DataSource = recn;
   this.DropDownList1.DataTextField="LastName";
   this.DropDownList1.DataValueField="EmployeeID";
   this.DropDownList1.DataBind();
   

   //绑定DATAGRID
   SqlConnection con =DataBaseDB.createCon();
   SqlDataAdapter sda = new SqlDataAdapter();
   sda.SelectCommand = new SqlCommand("SELECT * FROM Employees");
   sda.SelectCommand.Connection = con;
   DataSet ds = new DataSet();
   sda.Fill(ds,"emp");        
   this.DataGrid1.DataSource = ds.Tables["emp"];
   this.DataGrid1.DataBind();
   //*/
  }
 

posted on 2006-10-31 15:42  你也叫风语者  阅读(277)  评论(0编辑  收藏  举报