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;
using System.Data.SqlClient;
namespace WebApplication1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Repeater Repeater1;
protected System.Web.UI.WebControls.TextBox tbName;
protected System.Web.UI.WebControls.TextBox tbAge;
protected System.Web.UI.WebControls.TextBox tbTelephone;
protected System.Web.UI.WebControls.TextBox tbAddress;
protected System.Web.UI.WebControls.DropDownList ddlEducation;
protected System.Web.UI.HtmlControls.HtmlTextArea taExperience;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.Button btnUpdate;
protected System.Web.UI.WebControls.Button btnDelete;
protected System.Web.UI.WebControls.Button btnClose;
protected System.Web.UI.WebControls.DropDownList ddlSex;
protected System.Web.UI.WebControls.Label lbMessage;
protected System.Web.UI.WebControls.Button btnAdd;
public void Binder()//显示数据绑定函数
{
string Connstring = "database=test;password=;user=sa;server=.";
string Sqlstring = "SELECT * FROM UserInformation ORDER BY Name";
SqlConnection MyConn = new SqlConnection(Connstring);
SqlDataAdapter MyDa = new SqlDataAdapter(Sqlstring,MyConn);
DataSet MyDs = new DataSet();
MyDa.Fill(MyDs,"UserInformation");
Repeater1.DataSource=MyDs;
Repeater1.DataBind();
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
this.tbName.Text="";
this.tbAddress.Text="";
this.taExperience.Value="";
this.ddlEducation.SelectedItem.Text="";
this.ddlSex.SelectedItem.Text="";
this.tbTelephone.Text="";
this.tbTelephone.Text="";
}
Binder();//绑定数据
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnAdd_Click(object sender, System.EventArgs e)
{ //增加一个用户
string Connstring = "database=test;password=;user=sa;server=.";
string Sqlstring = "SELECT * FROM UserInformation WHERE Name='"+this.tbName.Text+"'";
SqlConnection MyConn = new SqlConnection(Connstring);
SqlDataAdapter MyDa = new SqlDataAdapter(Sqlstring,MyConn);
DataSet MyDs = new DataSet();
MyDa.Fill(MyDs,"UserInformation");
if(MyDs.Tables["UserInformation"].Rows.Count>0)
{
this.lbMessage.Text="<font color=red>"+"该人已经存在,请换一个名字吧!"+"</font>";
}
else
{
if(Page.IsValid)
{
string Name = this.tbName.Text;
string Sex = this.ddlSex.SelectedItem.Text;
string Age = this.tbAge.Text;
string Telephone = this.tbTelephone.Text;
string Address = this.tbAddress.Text;
string Education = this.ddlEducation.SelectedItem.Text;
string Experience = this.taExperience.Value;
string Conn = "database=test;password=;user=sa;server=.";
string SqlAdd =string.Format( "INSERT INTO UserInformation VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",Name,Sex,Age,Education,Telephone,Address,Experience);
SqlConnection MyConnection = new SqlConnection(Conn);
SqlDataAdapter MyDataAdapter = new SqlDataAdapter(SqlAdd,MyConnection);
DataSet MyDataSet = new DataSet();
MyDataAdapter.Fill(MyDataSet,"UserInformation");
Binder();
}
}
}
private void btnUpdate_Click(object sender, System.EventArgs e)
{ //修改用户信息
string Connstring = "database=test;password=;user=sa;server=.";
string Sqlstring = "SELECT * FROM UserInformation WHERE Name='"+this.tbName.Text+"'";
SqlConnection MyConn = new SqlConnection(Connstring);
SqlDataAdapter MyDa = new SqlDataAdapter(Sqlstring,MyConn);
DataSet MyDs = new DataSet();
MyDa.Fill(MyDs,"UserInformation");
if(MyDs.Tables["UserInformation"].Rows.Count>0)
{
if(Page.IsValid)
{
string Name = this.tbName.Text;
string Sex = this.ddlSex.SelectedItem.Text;
string Age = this.tbAge.Text;
string Telephone = this.tbTelephone.Text;
string Address = this.tbAddress.Text;
string Education = this.ddlEducation.SelectedItem.Text;
string Experience = this.taExperience.Value;
string Conn = "database=test;password=;user=sa;server=.";
string SqlUpdate = "UPDATE UserInformation SET Name='"+Name+"',Sex='"+Sex+"',Age='"+Age+"',Telephone='"+Telephone+"',Address='"+Address+"',Education='"+Education+"',Experience='"+Experience+"'WHERE Name='"+Name+"'";
SqlConnection MyConnection = new SqlConnection(Conn);
SqlDataAdapter MyDataAdapter = new SqlDataAdapter(SqlUpdate,MyConnection);
DataSet MyDataSet = new DataSet();
MyDataAdapter.Fill(MyDataSet,"UserInformation");
Binder();
}
}
else
{
this.lbMessage.Text="<font color=red>"+"该人不存在!"+"</font>";
}
}
private void btnDelete_Click(object sender, System.EventArgs e)
{//删除用户
string Connstring = "database=test;password=;user=sa;server=.";
string Sqlstring = "SELECT * FROM UserInformation WHERE Name='"+this.tbName.Text+"'";
SqlConnection MyConn = new SqlConnection(Connstring);
SqlDataAdapter MyDa = new SqlDataAdapter(Sqlstring,MyConn);
DataSet MyDs = new DataSet();
MyDa.Fill(MyDs,"UserInformation");
if(MyDs.Tables["UserInformation"].Rows.Count>0)
{
if(Page.IsValid)
{
string Name = this.tbName.Text;
string Sex = this.ddlSex.SelectedItem.Text;
string Age = this.tbAge.Text;
string Telephone = this.tbTelephone.Text;
string Address = this.tbAddress.Text;
string Education = this.ddlEducation.SelectedItem.Text;
string Experience = this.taExperience.Value;
string Conn = "database=test;password=;user=sa;server=.";
string SqlDelete = "DELETE FROM UserInformation WHERE Name='"+this.tbName.Text+"'";
SqlConnection MyConnection = new SqlConnection(Conn);
SqlDataAdapter MyDataAdapter = new SqlDataAdapter(SqlDelete,MyConnection);
DataSet MyDataSet = new DataSet();
MyDataAdapter.Fill(MyDataSet,"UserInformation");
Binder();
}
}
else
{
this.lbMessage.Text="<font color=red>"+"该人不存在!"+"</font>";
}
}
private void btnClose_Click(object sender, System.EventArgs e)
{//关闭当前窗口
this.btnClose.Attributes.Add("Onclick","window.close();");
}
}
}
在进行上面的编码之前还应该建立一个数据库,建立数据库的代码如下:
create database test;
CREATE TABLE UserInformation(
Name varchar(20) NOT NULL,
Sex char(2),
Age int,
Education varchar(50),
Telephone char(11),
Address varchar(50),
Experience TEXT,
)
本文介绍一个通过WEB页面对数据库的操作的例子,本程序实现的功能是对数据库里的表进行插入,删除和修改的操作,还通过repeater控件将数据显示到WEB页面上。既然asp.net比asp优越的一个特性就是他的HTML标记和代码是分离的,那么我就在这里将这个两个文件的代码拷贝如下,下面是WebForm1.aspx文件的源代码:
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style type="text/css">.table { }
.td { BACKGROUND-COLOR: blue }
</style>
</HEAD>
<body>
<div align="center">
<form id="Form1" method="post" runat="server">
<asp:repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" cellpadding="0" cellspacing="0" border="0">
<caption>
<h3>人才信息表</h3>
</caption>
<tr>
<td>姓 名</td>
<td>性 别</td>
<td>年 龄</td>
<td>学 历</td>
<td>联系电话</td>
<td>通讯住址</td>
<td>工作经验</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr> <!--下面的代码中的Name,Sex,Age,Education,Telephone,Address,
Experience都是数据库表中一个字段,可以根据自己的具体情况修改
-->
<td><%#DataBinder.Eval(Container,"DataItem.Name")%></td>
<td><%#DataBinder.Eval(Container,"DataItem.Sex")%></td>
<td><%#DataBinder.Eval(Container,"DataItem.Age")%></td>
<td><%#DataBinder.Eval(Container,"DataItem.Education")%></td>
<td><%#DataBinder.Eval(Container,"DataItem.Telephone")%></td>
<td><%#DataBinder.Eval(Container,"DataItem.Address")%></td>
<td><%#DataBinder.Eval(Container,"DataItem.Experience")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
<br>
<br>
<asp:Label id="lbMessage" runat="server"></asp:Label>
<br>
<br>
<TABLE id="Table1" borderColor="blue" cellSpacing="0" cellPadding="0" width="465" align="center"
frame="vsides">
<TR class="td" height="80">
<TD style="WIDTH: 361px" align="center"></TD>
</TR>
<tr>
<td align="center">
<table cellSpacing="0" cellPadding="2" border="0">
<TR>
<TD>姓 名:</TD>
<TD><asp:textbox id="tbName" runat="server"></asp:textbox><asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" ErrorMessage="姓名不能为空" Font-Size="X-Small"
ControlToValidate="tbName"></asp:requiredfieldvalidator></TD>
</TR>
<TR>
<TD><FONT face="宋体">性 别:</FONT></TD>
<TD>
<asp:DropDownList id="ddlSex" runat="server">
<asp:ListItem>请选择</asp:ListItem>
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList></TD>
</TR>
<TR>
<TD><FONT face="宋体">年 龄:</FONT></TD>
<TD><asp:textbox id="tbAge" runat="server"></asp:textbox></TD>
</TR>
<TR>
<TD>学 历:</TD>
<TD><asp:dropdownlist id="ddlEducation" runat="server">
<asp:ListItem>请选择</asp:ListItem>
<asp:ListItem Value="博士">博士</asp:ListItem>
<asp:ListItem Value="硕士">硕士</asp:ListItem>
<asp:ListItem Value="学士">学士</asp:ListItem>
<asp:ListItem Value="大专">大专</asp:ListItem>
<asp:ListItem Value="高中">高中</asp:ListItem>
<asp:ListItem Value="文盲">文盲</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:dropdownlist></TD>
</TR>
<TR>
<TD><FONT face="宋体">联系电话:</FONT></TD>
<TD><asp:textbox id="tbTelephone" runat="server"></asp:textbox></TD>
</TR>
<TR>
<TD><FONT face="宋体">通讯地址:</FONT></TD>
<TD><asp:textbox id="tbAddress" runat="server"></asp:textbox></TD>
</TR>
<TR>
<TD><FONT face="宋体">工作经验:</FONT></TD>
<TD><TEXTAREA id="taExperience" style="WIDTH: 153px" name="TEXTAREA1" rows="5" cols="16" runat="server"> </TEXTAREA></TD>
</TR>
<TR>
<TD align="center" colSpan="2">
<asp:Button id="btnUpdate" runat="server" Text="修改"></asp:Button>
<asp:button id="btnAdd" runat="server" Text="添加"></asp:button>
<asp:Button id="btnDelete" runat="server" Text="删除"></asp:Button>
<asp:Button id="btnClose" runat="server" Text="关闭"></asp:Button></TD>
</TR>
</table>
</td>
</tr>
<TR height="80">
<TD class="td"></TD>
</TR>
</TABLE>
<br>
<br>
</form>
</div>
</body>
</HTML>