字段名 id name Zhiwu type Title Content data 数据类型 int char char char smalldata datetime char 4 50 50 50 4 50 长度 是否主键 是
4.2利用ADO访问数据库
在Web动态交互过程中,经常需要根据用户输入的查询条件来查询数据库,从数据库中取出结果并以一定的格式显示给用户。有时也需要从用户那里收集信息,并按一定的格式保存到数据库里。本系统采用SQL2000数据库,下面介绍本系统数据库的连接方式。
⑴ 创建数据库链接(Connection)
ASP.Net文件中如果要访问数据,必须首先创建与数据库的连接,本系统数据库连接如下:
return new SqlConnection(\512\);这条语句打开链接,con
表示与数据源建立连接的相关信息,这里以与本系统的
数据库连接为例,server=(local)连接本地数据源,database选择要打开的数据库名,uid,pwd为数据库的用户名和密码。
以上两个步骤缺一不可,因为链接对象的创建与打开是两回事,只有打开了才真正可以使用。
4.3 各功能模块设计与实现 4.3.1 用户登录模块设计
14
using System; using System.Data;
using System.Configuration; using System.Collections; using System.Web;
using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class Login : System.Web.UI.Page {
SqlOperate sqloperate = new SqlOperate();
protected void Page_Load(object sender, EventArgs e) { }
protected void btnlogin_Click(object sender, EventArgs e) {
if (txtCode.Text.Trim() != Session[\].ToString()) {
Response.Write(\验证码错误');location='Login.aspx'\); } else {
if (this.ddlstatus.SelectedValue == \管理员\)
15
{
int insert = sqloperate.checkLogin1(txtNum.Text, txtPwd.Text); if (insert > 0) {
Response.Write(\登录成功!');history.go(-1);\);
Session[\] = txtNum.Text;
Response.Redirect(\); } else {
Response.Write(\您输入的用户名或密码错误,请重新输入!');history.go(-1);\); }
}
if (this.ddlstatus.SelectedValue == \教师\) {
int insert = sqloperate.checkLogin2(txtNum.Text, txtPwd.Text); if (insert > 0) {
Response.Write(\登录成功!');history.go(-1);\);
Session[\] = txtNum.Text;
Response.Redirect(\); } else {
Response.Write(\您输入的用户名或密码错误,请重新输入!');history.go(-1);\); } } } }
protected void btnconcel_Click(object sender, EventArgs e) {
RegisterStartupScript(\提示\, \); } }
16
protected void Button1_Click(object sender, EventArgs e) { try {
string sqlstr = \
tb_Teacher(type,name,password,birth,sex,educate,zhicheng,address,phone)values('\ + TextBox1.Text + \ + TextBox2.Text + \ + TextBox3.Text + \ + TextBox4.Text + \ +
DropDownList1.SelectedValue + \ + DropDownList2.SelectedValue + \ + TextBox5.Text + \ + TextBox6.Text + \ + TextBox7.Text + \; sqloperate.DataCom(sqlstr);
Response.Write(\教师信息发布成功!');document.location='uTeacherAdd.aspx'\);
} catch {
Response.Write(\对不起,你的信息发布失败!');history.go(-1);\); } }
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { }
17
public partial class user_uInfoManage : System.Web.UI.Page {
string sql;
SqlOperate sqloperate = new SqlOperate();
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
list_bind();
if (Request.QueryString[\] == \) {
del_zp(); } } }
public void del_zp()//删除信息 {
int id = int.Parse(Request.QueryString[\]); string sql = \ + id; Class1.ExecSql(sql);
Class1.AlertShow(\删除成功!\, \); }
private void list_bind() {
SqlConnection conn = Class1.DBconnection();
18