现在的位置: 首页 > 综合 > 正文

吉林大学asp.net–ado.net教程 第一章例题带解释

2011年09月07日 ⁄ 综合 ⁄ 共 5696字 ⁄ 字号 评论关闭

在default.aspx上分别有如果下控件
 文本框:
id,name,pwd,pwd2
-------------------------
radiobutton控件两个同在一组
nan,nv
--------------------------
按钮一个:
button1
------------------------
验证控件三个
验证非空控件一个,
验证数据库是否有相同数据控件两个,分别验证 id和name
验证pwd和pwd2字符是否一致控件一个
------------------------------------------------------------------
网格 datagrid 控件一个
--------------------------------------default.aspx.cs源文件---------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ado
{
 /// <summary>
 /// _default 的摘要说明。
 /// </summary>
 public class _default : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox name;
  protected System.Web.UI.WebControls.TextBox pwd;
  protected System.Web.UI.WebControls.TextBox id;
  protected System.Web.UI.WebControls.RadioButton nan;
  protected System.Web.UI.WebControls.RadioButton nv;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
  protected System.Web.UI.WebControls.CustomValidator CustomValidator2;
  protected System.Web.UI.WebControls.TextBox pwd2;
  protected System.Web.UI.WebControls.CompareValidator CompareValidator1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.CustomValidator CustomValidator1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   this.filldb();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.CustomValidator1.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator1_ServerValidate);
   this.CustomValidator2.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator2_ServerValidate);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
  {
   string pid=args.Value;//指明pid的值
   if(db.fiedname(pid))//如果db类的fiename返回的值是false,值为真,否则为假
   {
    args.IsValid=true;
   }
   else
   {
    args.IsValid=false;
   }
  }

  private void CustomValidator2_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
  {
   string name=args.Value;//同上,这个是限制第二个文本框不能输入相同的字符串
   if (db.finname(name))
   {
    args.IsValid=true;
   }
   else
   {
    args.IsValid=false;
   }
  }
  public void filldb()//做一个方法,方便页面的多处绑定
  {
  this.DataGrid1.DataSource=db.selectAll();//指明DataGrid1的数据源
   this.DataGrid1.DataBind();//绑定
  }
  private void Button1_Click(object sender, System.EventArgs e)//插入数据
  {
   db p=new db();
   p.pid=this.id.Text;
   p.pname=this.name.Text;
   p.ppwd=this.pwd.Text;
   if(this.nan.Checked)//如果nan被选中
   {
    p.psex="男";
   }
   else
   {
    p.psex="女";
   }
   if(db.insertcmd(p))
   {
    Response.Write("插入成功");
    this.filldb();//插入成功就调用绑定
   }
   else
   {
   Response.Write("失败"+e.ToString());//如果失败就显示并显示出捕获的错误信息
   }
  }
 }
}
----------------------------------------db.cs类源文件---------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;

namespace ado
{
 /// <summary>
 /// db 的摘要说明。
 /// </summary>
 public class db
 {
  //定义几个属性
  public string pid;
  public string pname;
  public string ppwd;
  public string psex;
  public db()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  //定义连接字符串
  public static SqlConnection createCon()
 {
 SqlConnection con=new SqlConnection("server=.;database=usern;uid=sa;pwd=980123;");
   return con;
 }
  public static bool fiedname(string id)//查找数据库中是否有相同ID
  {
   SqlConnection con=db.createCon();
   con.Open();
   SqlCommand cmd=new SqlCommand("select count(*) from proe where uid='"+id+"'",con);
   int count=Convert.ToInt32(cmd.ExecuteScalar());//返回首行首列
   if (count>0)
   {
    return false;
   }
   else
   {
    return true;
   }
  }
   public static bool finname(string name)//同上
   {
   SqlConnection con=db.createCon();
    con.Open();
   SqlCommand cmd1=new SqlCommand("select count(*) from proe where uname='"+name+"'",con);
    int count=Convert.ToInt32(cmd1.ExecuteScalar());
    if (count>0)
    {
     return false;
    }
    else
    {
    return true;
    }
   }
  public static bool insertcmd(db p)//插入操作
  {
   try//判断,如果下面插入操作成功执行就返回真
   {
    SqlConnection con=db.createCon();
    con.Open();
    SqlCommand cmdi=new SqlCommand("insert into proe values(@pid,@pname,@ppwd,@psex)",con);
    SqlParameter para=new SqlParameter("@pid",SqlDbType.VarChar,10);
    para.Value=p.pid;
    cmdi.Parameters.Add(para);
    para=new SqlParameter("@pname",SqlDbType.VarChar,50);
    para.Value=p.pname;
    cmdi.Parameters.Add(para);
    para=new SqlParameter("@ppwd",SqlDbType.VarChar,10);
    para.Value=p.ppwd;
    cmdi.Parameters.Add(para);
    para=new SqlParameter("@psex",SqlDbType.VarChar,2);//实例化SqlParameter("命令对象",数据类型,数据尺寸)
    para.Value=p.psex;//给出p.psex的值
    cmdi.Parameters.Add(para);//将p.psex添加到para集合
    cmdi.ExecuteNonQuery();//执行插入操作
    con.Close();//关闭连接
    return true;
   }
   catch(Exception e)//否则返回假并捕获错误
   {
    return false;
   }
  }
  public static DataTable selectAll()//声明一静态检索对象
  {
  SqlConnection con=db.createCon();//指明连接,下面 不需要打开,如果没有开,下面语句有钥匙,会自己开
   SqlDataAdapter sda=new SqlDataAdapter();//创建数据适配器对象
  sda.SelectCommand=new SqlCommand("select * from proe",con);//实例化检索数据的SelectCommand对象
  DataSet ds=new DataSet();//指明一个数据集
   sda.Fill(ds,"proe");//使用fill填充本地虚拟表
   return ds.Tables["proe"];//返回给页面
  }
 }
}
 

--------------------------------------完---------------------------------------

抱歉!评论已关闭.