第四讲 数据开发.pptVIP

  • 15
  • 0
  • 约1.39万字
  • 约 34页
  • 2016-12-11 发布于河南
  • 举报
第四讲 数据库开发实例 数据库开发实例 建立数据库:数据表 数据库开发实例 1、设置config文件 appSettingsadd key=ConnectionString value=Server=(local);user id=saews;pwd=123;DataBase=dbSAEWS;pooling=true/add /appSettings 数据库开发实例 2、添加“类”文件并在其中添加相关底层代码(默认名class1) 3、 “类”文件添加相关引用:using System.Data.SqlClient; static public bool ExecuteSql(string sqlStr){SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[ConnectionString]);try{SqlCommand com = new SqlCommand(sqlStr, conn);conn.Open();com.ExecuteNonQuery();conn.Close();return true;}catch (Exception ee){conn.Close();return false;}} 实例1——用户登录 static public DataSet GetDsFromSql(string sqlStr){SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[ConnectionString]);DataSet ds = new DataSet();try{SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn);conn.Open();da.Fill(ds);}catch (Exception){ds.Tables.Add(TempTable);}conn.Close();return ds;} 实例1——用户登录 protected void btnOK_Click(object sender, EventArgs e){string sql;sql = select * from db_User where UserID= + txtName.Text + and Password= + txtPsw.Text+;DataTable dt;dt= Class1.GetDsFromSql(sql).Tables[0];if(dt.Rows.Count0){lbl1.Text=恭喜您登陆成功!你的密码是:+ dt.Rows[0][Password].ToString();}else{lbl1.Text=请输入的用户名和密码不正确!;}} 实例2—DropDownList案例 建立数据表 实例2—DropDownList案例 protected void Page_Load(object sender, EventArgs e){//绑定省份DropDownList案例if (!this.IsPostBack){DataBindProvince();//绑定省份}} 实例2—DropDownList案例 protected void DataBindProvince(){string sql;sql = select * from db_Province ;DataTable dt;dt = Class1.GetDsFromSql(sql).Tables[0];dpProvince.DataSource = dt;dpProvince.DataBind();} 实例3—DataList案例 asp:DataList ID=dlUniversityProv runat=server RepeatDirection=horizontal RepeatColumns=3ItemTemplatetable cellpadding=0 cellspacing=0 width=350tr style=height:35px;td style=width:95%;a id=noticetitle target=_blank href=%# DataBinder.Eval(Container.DataItem,UniversityURL) %%# DataBinder.Eval(Container.DataItem,UniversityName) %/a/td/tr/table/ItemTemplate/asp:DataList 实例3—DataList案例 protected

文档评论(0)

1亿VIP精品文档

相关文档