科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航



ZDNet>软件频道>中间件-zhiding>[冷枫]一个不错的.NET例子

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

一个不错的.NET例子

来源:CSDN 2007年09月23日

关键字:冷枫


数据浏览 
对TextBox进行简单绑定



  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!IsPostBack)
   {
    Session["RecordPos"] = 0;   
   }
   sqlDataAdapter1.Fill(dsStudent1.tbStudentinfo);
   this.DataBind();
  }

  private void btnPrev_Click(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   if (RecordPos > 0)
   {
    RecordPos--;
   }
   Session["RecordPos"] = RecordPos;
   this.DataBind();
  }

  private void btnNext_Click(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   RecordPos++;
   if (RecordPos >= dsStudent1.tbStudentinfo.Rows.Count)
   {
    RecordPos=0;
   }
   Session["RecordPos"] = RecordPos;
   this.DataBind();
  }

  private void tbID_DataBinding(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   tbID.Text = dsStudent1.tbStudentinfo.Rows[RecordPos]["StudentID"].ToString();
  }
  private void tbName_DataBinding(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   tbName.Text = dsStudent1.tbStudentinfo.Rows[RecordPos]["StudentName"].ToString();
  }
  private void tbScore_DataBinding(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   tbScore.Text = dsStudent1.tbStudentinfo.Rows[RecordPos]["Score"].ToString();
  }

 


简单连接的例子
简单绑定一个Label
<%@ Import Namespace="System.Drawing" %>    调入一个命名空间
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!IsPostBack)
   {
    ArrayList ar = new ArrayList();
    ar.Add("Red");
    ar.Add("Blue");
    ar.Add("Green");
    ar.Add("Orange");
    ddlColor.DataSource = ar;
   }
   this.DataBind();
  }

 


  private void btnAdd_Click(object sender, System.EventArgs e)
  {
   string strSql = "insert into tbUserInfo values('"+tbName.Text+"','"+tbPass.Text+"',"+"'"+trblSex.SelectedItem.Text+"'"+",";
   strSql += " '"+cdDate.SelectedDate.ToString()+"','"+tbZhiWei.Text+"','"+tbNotes.Text+"')";
   ExcuteSql(strSql);
  }
  private void ExcuteSql(string strSql)
  {
   try
   {
    SqlConnection con = new SqlConnection();
    con.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
    con.Open();
    //添加纪录...
    SqlCommand com = new SqlCommand(strSql,con);
    com.ExecuteNonQuery();
    con.Close();
    Response.Write("<script language='javascript'>alert('插入成功!')</script>");
   }
   catch(Exception ee)
   {
    Response.Write("发生错误:"+ee.Message);
   }
  }


  private void btnLogin_Click(object sender, System.EventArgs e)
  {
   SqlConnection con = new SqlConnection();
   con.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
   con.Open();
  
   string strSql = "select UserName,UserPass from tbUserInfo where UserName='"+tbName.Text+"' and UserPass='"+tbPass.Text+"'";
   SqlCommand com = new SqlCommand(strSql,con);
   SqlDataReader dr = com.ExecuteReader();
   //以下执行查询
   bool bExist = false;
   while(dr.Read())
   {
    bExist = true;
    Session["UserName"] = dr.GetString(0);
    Session["UserPass"] = dr.GetString(1);
   }
   if(bExist)
    Response.Redirect("023Query.aspx");
   else
    Response.Write("<script language='javascript'>alert('用户名称或密码错误!')</script>");

   con.Close();
  }

private void Page_Load(object sender, System.EventArgs e)
  {
   lbUserName.Text = Session["UserName"].ToString();
  }
  private void btnQuery_Click(object sender, System.EventArgs e)
  {
   SqlConnection con = new SqlConnection();
   con.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
   con.Open();
   string strSql = "select EnterDate from tbUserInfo where UserName='"+tbName.Text+"'";
   SqlCommand com = new SqlCommand(strSql,con);
  
   com.Parameters.Add("@Name",SqlDbType.VarChar,50,"UserName");
   com.Parameters["@Name"].Value = tbName.Text;
   com.CommandText = "select EnterDate from tbUserInfo where UserName=@Name";

   object obj = com.ExecuteScalar();
   if(obj!=null)
    lbHireDate.Text = obj.ToString();
   else
    lbHireDate.Text = "数据库中没有此人!";
   con.Close();
  }

 


  private void Page_Load(object sender, System.EventArgs e)
  {
   sqlDataAdapter1.Fill(dsUser1.tbUserInfo);
   if(!IsPostBack)
   {   
    Session["RecordPos"] = 0;

    this.DataBind();
   }  
  }
  private void ExcuteSql(string strSql)
  {
   try
   {
    SqlConnection con = new SqlConnection();
    con.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
    con.Open();
    //添加纪录...
    SqlCommand com = new SqlCommand(strSql,con);
    com.ExecuteNonQuery();
    con.Close();
    //Response.Write("<script language='javascript'>alert('操作成功!')</script>");
   }
   catch(Exception ee)
   {
    Response.Write("发生错误:"+ee.Message);
   }
  }
//下一页
  private void btnNext_Click(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   RecordPos++;
   if (RecordPos >= dsUser1.tbUserInfo.Rows.Count)
   {
    RecordPos=0;
   }
   Session["RecordPos"] = RecordPos;
   this.DataBind();
  }
//上一页
  private void btnPrev_Click(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   if (RecordPos > 0)
   {
    RecordPos--;
   }
   Session["RecordPos"] = RecordPos;
   this.DataBind();
  }
//数据绑定
  private void tbName_DataBinding(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   tbName.Text = dsUser1.tbUserInfo.Rows[RecordPos]["UserName"].ToString();
  }
  private void tbPass_DataBinding(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   tbPass.Text = dsUser1.tbUserInfo.Rows[RecordPos]["UserPass"].ToString();
  }
  private void tbSex_DataBinding(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   tbSex.Text = dsUser1.tbUserInfo.Rows[RecordPos]["Sex"].ToString();
  }
  private void tbZhiWei_DataBinding(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   tbZhiWei.Text = dsUser1.tbUserInfo.Rows[RecordPos]["zhiwei"].ToString();
  }
  private void tbNotes_DataBinding(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   tbNotes.Text = dsUser1.tbUserInfo.Rows[RecordPos]["Notes"].ToString();
  }
  private void tbDate_DataBinding(object sender, System.EventArgs e)
  {
   int RecordPos = (int) Session["RecordPos"];
   tbDate.Text = dsUser1.tbUserInfo.Rows[RecordPos]["EnterDate"].ToString();
  }
//删除
  private void btnDelete_Click(object sender, System.EventArgs e)
  {
   string strSql = "Delete tbUserInfo where username='"+tbName.Text+"'";
   ExcuteSql(strSql);
  }
//更新
  private void btnUpdate_Click(object sender, System.EventArgs e)
  {
   string strSql = "UPDATE tbUserInfo "+
    "SET " +
    "UserPass ='"+tbPass.Text+"', "+
    "Sex ='"+tbSex.Text+"'," +
    "EnterDate ='"+tbDate.Text+"'," +
    "Zhiwei ='"+tbZhiWei.Text+"'," +
    "Notes ='" + tbNotes.Text + "'" +
    " WHERE UserName ='"+tbName.Text+"'";
   ExcuteSql(strSql);
  }

 

有关存储过程
//执行存储过程
  private void btnDataReader_Click(object sender, System.EventArgs e)
  {
   //建立Table,用来存放查询的结果
   DataTable table = new DataTable("Test");
   //为表格添加列
   table.Columns.Add("EmployeeID");
   table.Columns.Add("OrderDate");
   table.Columns.Add("RequiredDate");
   //执行查询
   SqlDataReader rdrEmployees;
   sqlConnection.Open();//打开数据库连接
   rdrEmployees = sqlCommand.ExecuteReader();
//执行语句
   //以下读取数据
   DataRow   dr;
   while(rdrEmployees.Read())
   {
    dr = table.NewRow();//新建一行
    //对当前行每一列赋值
    dr[0] = rdrEmployees.GetInt32(0);
    dr[1] = rdrEmployees.GetDateTime(1);
    dr[2] = rdrEmployees.GetDateTime(2);
    table.Rows.Add(dr);//添加当前行
   }
   //以下设置数据源
   sqlConnection.Close();//关闭连接
   dataGridReader.DataSource = table;//设置数据源,显示数据
   dataGridReader.DataBind();
  }

//执行带参数的存储过程
  private void Button1_Click(object sender, System.EventArgs e)
  {
   //建立Table,用来存放查询的结果
   DataTable table = new DataTable("Test");
   //为表格添加列
   table.Columns.Add("OrderID");
   table.Columns.Add("OrderDate");
   table.Columns.Add("RequreDate");
   table.Columns.Add("ShippedDate");
  //执行查询
   SqlDataReader rdrEmployees;
   sqlConnection.Open();//打开数据库连接
   sqlCommand1.Parameters["@CustomerID"].Value = "VICTE";
   sqlCommand1.Parameters["@RETURN_VALUE"].Value = 1;
   rdrEmployees = sqlCommand1.ExecuteReader();
//执行语句
   //以下读取数据
   DataRow   dr;
   while(rdrEmployees.Read())
   {
    dr = table.NewRow();//新建一行
    //对当前行每一列赋值
    dr[0] = rdrEmployees.GetInt32(0);
    dr[1] = rdrEmployees.GetDateTime(1);
    dr[2] = rdrEmployees.GetDateTime(2);
    dr[3] = rdrEmployees.GetDateTime(3);
    table.Rows.Add(dr);//添加当前行
   }
   //以下设置数据源
   sqlConnection.Close();//关闭连接
   dataGridReader.DataSource = table;//设置数据源,显示数据
   dataGridReader.DataBind();  
  }
 


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1039063

推广二维码
邮件订阅

如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

重磅专题