创建DBhelp.cs,编写连接数据库的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
///DBhelp 的摘要说明
/// </summary>
public static class DBhelp
{
static string str = "server=.;database=StudentSystem;Trusted_Connection=true;";
public static SqlConnection conn = new SqlConnection(str);
public static SqlCommand comm = new SqlCommand();
}
一:GridView控件的运用
1.创建登录页面:studentLogin.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="studentLogin.aspx.cs" Inherits="student" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
.style1
{
height:60px;
width:300px;
}
.style2
{
height: 63px;
width: 300px;
}
</style>
</head>
<body style="background-image:url('images/1.png');">
<form id="form1" runat="server">
<p align="center" style="font-size:35px;">学生信息管理系统</p>
<div style="padding-top:100px;">
<table align="center" style="width:500px;border:solid 8px black; text-align:center;">
<tr>
<td colspan="2" style="font-size:large;background-color:green; height:60px;" align="center">用户登录
</td>
</tr>
<tr>
<td style="font-size:large" class="style1">用户名</td>
<td class="style1"><asp:TextBox ID="UserName" runat="server" Width="188px"></asp:TextBox></td>
</tr>
<tr>
<td style="font-size:large" class="style1">密码</td>
<td class="style1"><asp:TextBox ID="UserPwd" runat="server" Width="190px"></asp:TextBox></td>
</tr>
<tr>
<td class="style2">
<asp:Button ID="Button1" runat="server" Text="登录" Height="27px"
Width="70px" onclick="Button1_Click" /></td>
<td class="style2">
<asp:Button ID="Button2" runat="server" Text="取消" Height="27px"
Width="70px" onclick="Button2_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
双击登录按钮,里面代码:
protected void Button1_Click(object sender, EventArgs e)
{
string str = "server=.;database=studentInf;Trusted_Connection=true;";
SqlConnection con = new SqlConnection(str);
con.Open();
string sqlstr =string.Format("select * from tb_User where UserName='{0}' and UserPasswd='{1}'",UserName.Text,UserPwd.Text);
SqlCommand cmd = new SqlCommand(sqlstr, con);
if (cmd.ExecuteScalar() == null)
{
Response.Write("<script>alert('用户名或密码错误!');</script>");
}
else
{
Response.Redirect("studentManager.aspx");
}
con.Close();
}
双击关闭按钮:
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("<script>window.close();</script>");
}
2.创建学生信息管理StudentTable.aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StudentTable.aspx.cs" Inherits="StudentTable" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 353px;
height:20px;
text-align: right;
}
.style2
{
height: 45px;
text-align:center;
}
.style4
{
height: 70px;
}
.c2
{
text-align: left;
}
</style>
</head>
<body>
<form id="form1" runat="server" style="text-align:center;">
<table style="width:800px; height:600px; border:1px solid black;" align="center">
<tr>
<td class="style1">
学号:</td>
<td class="c2">
<asp:TextBox ID="txtNo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
姓名:</td>
<td class="c2">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
性别:</td>
<td class="c2">
<asp:RadioButton ID="rbtnBoy" runat="server" GroupName="sex" Text="男" />
<asp:RadioButton ID="rbtnGirl" runat="server" Text="女" />
</td>
</tr>
<tr>
<td class="style1">
年龄:</td>
<td class="c2">
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
系别:</td>
<td class="c2">
<asp:TextBox ID="txtDept" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2" colspan="2">
<asp:Button ID="Button1" runat="server" Text="添加" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="修改" onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="删除" onclick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="查询" onclick="Button4_Click" />
</td>
</tr>
<tr>
<td class="style4" colspan="2">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" HorizontalAlign="Center"
Width="545px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Sno" HeaderText="学号" />
<asp:BoundField DataField="Sname" HeaderText="姓名" />
<asp:BoundField DataField="Sex" HeaderText="性别" />
<asp:BoundField DataField="Age" HeaderText="年龄" />
<asp:BoundField DataField="Dept" HeaderText="系别" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</td>
</tr>
</table>
<div>
</div>
</form>
</body>
</html>
设计图:
里面功能代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class StudentTable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//判断是否第一次访问
{
this.DataBd("");//默认显示表中的全部内容
}
}
//添加查询条件的方法
void DataBd(string where)
{
DBhelp.conn.Open(); //调用DBhelp类连接数据库
string sqlStr = "select * from student " + where;
SqlCommand cmd = new SqlCommand(sqlStr, DBhelp.conn);
SqlDataAdapter dapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dapt.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
DBhelp.conn.Close();
}
//执行增删该的方法
bool Excute(string sqlStr)
{
try
{
DBhelp.conn.Open();
SqlCommand cmd = new SqlCommand(sqlStr, DBhelp.conn);
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
DBhelp.conn.Close();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
string sNo = txtNo.Text;
string sName = txtName.Text;
//其他条件请思考,模糊查询
string where = " where 1=1 ";
where += sNo == "" ? "" : " and sNo like '%" + sNo + "%' ";
where += sName == "" ? "" : " and sName like '%" + sName + "%' ";
this.DataBd(where);
try
{
DBhelp.conn.Open();
string str = string.Format("select * from student where Sno='{0}'",txtNo.Text);
SqlCommand comm = new SqlCommand(str, DBhelp.conn);
SqlDataReader read = comm.ExecuteReader();
if (read.Read())
{
txtName.Text = read[1].ToString();
/* if(read[2].ToString=="男")
{
rbtnBoy.Checked;
}
else{
rbtnGirl.Checked;
}*/
txtAge.Text = read[3].ToString();
txtDept.Text = read[4].ToString();
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally
{
DBhelp.conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//获取学生的学号、姓名等相关信息
string sNo = txtNo.Text;
string sName = txtName.Text;
string sSex = "";
if (rbtnBoy.Checked)
sSex = "男";
if (rbtnGirl.Checked)
sSex = "女";
int sAge = int.Parse(txtAge.Text.Trim());
string dept = txtDept.Text;
//要执行的SQL语句
// string sqlStr = "insert into student(sNo,sName,sSex,sAge,dept) values('" + sNo + "','" + sName + "','" + sSex + "'," + sAge + ",'" + dept + "')";
string sqlStr = string.Format("insert into student(Sno,Sname,Age,Sex ,Dept) values('{0}', '{1}', {2}, '{3}', '{4}'); ", sNo, sName, sAge, sSex, dept);
//调用自定义方法Excute()执行插入命令
if (this.Excute(sqlStr))//调用Excute()方法
{
Response.Write("<script>alert('插入成功')</script>");
this.DataBd("");
}
else
{
Response.Write("<script>alert('数据出错!')</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string sNo = txtNo.Text;
if (sNo != "")
{
string sName = txtName.Text;
string sSex = "";
if (rbtnBoy.Checked)
sSex = "男";
if (rbtnGirl.Checked)
sSex = "女";
int sAge = int.Parse(txtAge.Text.Trim());
string dept = txtDept.Text;
string sqlStr = "update student set Sname='" + sName + "',Sex='" + sSex + "',Age=" + sAge + ",Dept='" + dept + "' where Sno='" + sNo + "'";
if (this.Excute(sqlStr))
{
Response.Write("<script>alert('修改成功')</script>");
this.DataBd("");
}
else
{
Response.Write("<script>alert('数据出错!')</script>");
}
}
else
{
Response.Write("<script>alert('学号不能为空')</script>");
}
}
protected void Button3_Click(object sender, EventArgs e)
{
string sNo = txtNo.Text;
if (sNo != "")
{
string sqlStr = "delete from student where Sno='" + sNo + "'";
if (this.Excute(sqlStr))
{
Response.Write("<script>alert('删除成功')</script>");
this.DataBd("");
}
else
{
Response.Write("<script>alert('数据出错!')</script>");
}
}
else
{
Response.Write("<script>alert('学号不能为空')</script>");
}
}
}
二:
DataList控件的运用:
创建DataList.aspx,代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="DataList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Both" Width="650px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<HeaderTemplate>
<table class="auto-style1">
<tr>
<td style="width :80px">编号</td>
<td style="width :400px">标题</td>
<td style="width :140px">用户</td>
<td style="width :80px">详细信息</td>
</tr>
</table>
</HeaderTemplate>
<ItemStyle BackColor="White" ForeColor="#003399" />
<ItemTemplate>
<table class="auto-style1">
<tr>
<td style="width :80px">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("postID") %>'></asp:Label>
</td>
<td style="width :400px"><%# Eval("postTitle") %></td>
<td style="width :140px"><%# Eval("userName") %></td>
<td style="width :80px"><a href='chakanPage.aspx?pid=<%# Eval("postID") %>'>查看</a></td>
</tr>
</table>
</ItemTemplate>
<SelectedItemStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
</asp:DataList>
<br />
<table class="auto-style1">
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">首页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">上一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">下一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click">尾页</asp:LinkButton>
</td>
<td>当前【<asp:Label ID="lbCurrent" runat="server" Text="1"></asp:Label>
】页</td>
<td>总页数【<asp:Label ID="lbCount" runat="server"></asp:Label>
】</td>
</tr>
</table>
</div>
</form>
</body>
</html>
功能代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class DataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ListBd();
}
}
void ListBd()
{
// DBhelp.conn.Open();
//string sqlStr = "select * from tbpost ";
//SqlCommand cmd = new SqlCommand(sqlStr, con);
//SqlDataAdapter dapt = new SqlDataAdapter(cmd);
//DataSet ds = new DataSet();
//dapt.Fill(ds);
//DataList1.DataSource = ds;
//DataList1.DataBind();
//con.Close();
try
{
DBhelp.conn.Open(); //调用DBhelp类连接数据库
string sqlStr = "select * from tbPost";
SqlCommand cmd = new SqlCommand(sqlStr, DBhelp.conn);
SqlDataAdapter dapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dapt.Fill(ds);
//创建一个PagedDataSource对象,用来处理分页
PagedDataSource pds = new PagedDataSource();
int currentPage = int.Parse(lbCurrent.Text); //获取当前页
pds.DataSource = ds.Tables[0].DefaultView; //获取pds对象的数据源
pds.AllowPaging = true; //允许分页
pds.PageSize = 3; //每页显示3条记录
pds.CurrentPageIndex = currentPage - 1; //设置当前页的索引值
//设置4个LinkButton控件的初始值
LinkButton1.Enabled = true;
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
//当前页为第一页,首页和上一页将不可用
if (currentPage == 1)
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
}
//当前页为尾页,尾页和下一页将不可用
if (currentPage == pds.PageCount)
{
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
}
lbCount.Text = Convert.ToString(pds.PageCount);
DataList1.DataSource = pds;
//绑定DataList控件,数据源为PagedDataSource对象
DataList1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
DBhelp.conn.Close();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
lbCurrent.Text = "1";
this.ListBd();
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
lbCurrent.Text = Convert.ToString(Convert.ToInt32(lbCurrent.Text) - 1);
this.ListBd();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
lbCurrent.Text = Convert.ToString(Convert.ToInt32(lbCurrent.Text) + 1);
this.ListBd();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
lbCurrent.Text = lbCount.Text;
this.ListBd();
}
}
设计页面:
结果:
点击下一页:
点击尾页:
创建查看页面:chakanPage.aspx,代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chakanPage.aspx.cs" Inherits="chakanPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 680px; height: 324px">
<tr>
<td style="width: 100px">
主题</td>
<td colspan="3">
<asp:TextBox ID="txtTitle" runat="server" Width="478px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
发帖时间:</td>
<td style="width: 91px">
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox></td>
<td style="width: 100px">
发帖用户:</td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 100px; height: 90px">
内容</td>
<td colspan="3" style="height: 90px">
<asp:TextBox ID="txtContent" runat="server" Height="192px" TextMode="MultiLine" Width="512px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 91px">
</td>
<td style="width: 100px">
</td>
<td style="width: 100px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class chakanPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DataBd();
}
}
void DataBd()
{
DBhelp.conn.Open(); //调用DBhelp类连接数据库
int postID = Convert.ToInt32(Request["pid"].ToString());
string sqlStr = "select * from tbpost where postID=" + postID;
//SqlCommand cmd = new SqlCommand(sqlStr, DBhelp.conn);
DBhelp.comm.CommandText = sqlStr;
DBhelp.comm.Connection = DBhelp.conn;
SqlDataAdapter dapt = new SqlDataAdapter(DBhelp.comm);
DataSet ds = new DataSet();
dapt.Fill(ds);
txtTitle.Text = ds.Tables[0].Rows[0]["postTitle"].ToString();
txtName.Text = ds.Tables[0].Rows[0]["userName"].ToString();
txtDate.Text = ds.Tables[0].Rows[0]["postdate"].ToString();
txtContent.Text = ds.Tables[0].Rows[0]["postConent"].ToString();
DBhelp.conn.Close();
}
}
设计页面:
点击第一页的查看按钮: