1、1软件工程课程设计题 目 学校教材订购系统编码 学 院 长安学院 专 业 软件工程 班级 09421 学 号 052 050 060 学生姓名 范文标 罗宇航 田翼彪 指导教师 刘锋 编写日期 2011-5-27 2目录:目录: 21 登陆界面 .32 用 datagrid 显示数据 .43 添加历史书目 .54 删除记录 93主要代码1 登陆界面/当单击确定按钮时,连接数据库 ,确定输入的帐号和密码是否匹配 ,跳转到书目的页面,若不匹配,则弹出帐号错误或密码错误的提示/具体代码如下:Private Sub button1_Click(ByVal sender As System.Object
2、, ByVal e As System.EventArgs) Handles button1.ClickDim str As StringDim passstr As Stringpassstr = Trim(CStr(TextBox2.Text)str = “ uid=sa;pwd=;database=book“Dim myconn As New SqlClient.SqlConnection(connstr)Dim da As New SqlClient.SqlDataAdapter(sql, myconn)Dim ds As New DataSetda.Fill(ds, “adminli
3、st“)Dim dacomm As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(da)If ds.Tables(“adminlist“).Rows.Count = 1 Then判断帐号密码是否和数据库匹配4If passstr = Trim(ds.Tables(“adminlist“).Rows(0)(“密码“) ThenResponse.Redirect(“book.aspx“)ElseResponse.Write(“密码错误!“)End IfElseResponse.Write(“用户名不存在!“)End If
4、End SubEnd Class2 用 datagrid 显示数据/首先建立 datagrid,通过连接数据库,将数据显示在 datagrid 中/具体代码如下:Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loadbinddata()End SubSub binddata()Dim myconnection As New SqlConnection(“initial catalog=book;uid=sa;pwd=“)Const strsql A
5、s String = “select * from book08 order by 书号“Dim mycommand As New SqlCommand(strsql, myconnection)Dim myda As New SqlDataAdapter5myda.SelectCommand = mycommandDim myds As New DataSetmyda.Fill(myds)DataGrid1.DataSource = mydsDataGrid1.DataBind()End Sub3 添加历史书目/history.aspx 在登陆系统并选择插入历史书本后,进入添加历史书目的页面
6、,决定选择哪条记录进行添加/在单击左侧的选择后,自动跳转到 update1.aspx,根据所选择的书目,进行相应的修改后添加到记录中去/具体代码如下:Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadIf Not Page.IsPostBack Thenbinddata()End IfEnd Sub连接数据库: Sub binddata()Dim myconnection As New SqlConnection(“initial catalo
7、g=book;uid=sa;pwd=“)Const strsql As String = “select * from book07 order by 书号”Dim mycommand As New SqlCommand(strsql, myconnection)Dim myda As New SqlDataAdapterMyda.SelectCommand = mycommand6Dim myds As New DataSetMyda.Fill(myds)DataGrid1.DataSource = mydsDataGrid1.DataBind()End Sub确定所选择的行的数据,并把该行
8、相应的内容分别通过session变量储存下来。Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommandDim b_no As TableCell = e.Item.Cells(1)Dim b_name As TableCell = e.Item.Cells(2)Dim b_a As TableCell = e.Item.Cells(3)Dim b_b A
9、s TableCell = e.Item.Cells(4)Dim b_c As TableCell = e.Item.Cells(5)Session(“a”) = b_no.TextSession(“b”) = b_name.TextSession(“c”) = b_a.TextSession(“d”) = b_b.TextSession(“e”) = b_c.TextResponse.Redirect(“history1.aspx”)End SubButton_click事件,重定向到book.aspx页面 Private Sub Button1_Click(ByVal sender As
10、System.Object, ByVal e As System.EventArgs) Handles Button1.Click7Response。Redirect(“book.aspx”)End SubEnd class/history1.aspx 页面:通过上一个页面的选择后,该页面进行修改并添加的操作/当管理员修改完成后,则可单击确定添加操作,将数据添加到 11 年的数据库中/具体代码如下:页面登陆代码:Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles My
11、Base.LoadIf Not IsPostBack ThenDim str As Stringstr = “ uid=sa;pwd=;database=book”Dim myconn As New SqlClient.SqlConnection(connstr)Dim da As New SqlClient.SqlDataAdapter(sql, myconn)Dim ds As New DataSetDa.Fill(ds, “book11”)Dim dacomm As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder
12、(da)8Label6.Text = ds.Tables(“book11”).Rows(0)(“书号”)Label7.Text = ds.Tables(“book11”).Rows(0)(“书名”)Label8.Text = ds.Tables(“book11”).Rows(0)(“作者”)TextBox1.Text = ds.Tables(“book11”).Rows(0)(“征定时间”)TextBox2.Text = ds.Tables(“book11”).Rows(0)(“数量”)End IfEnd Sub修改后添加到数据库的代码:Private Sub Button1_Click(By
13、Val sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim str As StringDim litem As ListItemstr = “ uid=sa;pwd=;database=book”Dim myconn As New SqlConnection(connstr)Dim da As New SqlDataAdapter(sql, myconn)Dim ds As New DataSetda。Fill(ds, “book08”)Dim myrow As DataRowDim da
14、comm As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(da)If ds.Tables(“book08”).Rows.Count = 1 ThenResponse.Write(“此书已存在”)End Ifmyrow = ds.Tables(“book11”).NewRowmyrow(“书号”) = Trim(Label6.Text)myrow(“书名”) = Trim(Label7.Text)9myrow(“作者”) = Trim(Label8.Text)myrow(“征定时间”) = Trim(TextBox
15、1.Text)myrow(“数量”) = Trim(TextBox2.Text)ds.Tables(“book11”).Rows.Add(myrow)da.UpdateCommand = dacomm.GetUpdateCommandda.Update(ds, “book11”)Response.Redirect(“book.aspx”)End Sub单击返回按钮后,重定向到history.aspx的代码:Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B
16、utton2.ClickResponse.Redirect(“history.aspx”)End SubEnd Class4 删除记录/在登陆系统后点击修改按钮进入修改页面 delete.aspx,通过复选框的选择,删除数据/该页面通过复选框的操作删除数据后,重定向回该页面,以便显示删除后数据的更新/具体代码如下:页面登陆代码:Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadIf Not Page.IsPostBack Then 用来确认页面
17、是否第一次登陆10binddata()End IfEnd SubSub binddata() 连接到数据库Dim myconnection As New SqlConnection(“initial catalog=book;uid=sa;pwd=“)Const strsql As String = “select * from book08 order by 书号”Dim mycommand As New SqlCommand(strsql, myconnection)Dim myda As New SqlDataAdapterMyda.SelectCommand = mycommandDi
18、m myds As New DataSetMyda.Fill(myds)DataGrid1.DataSource = mydsDataGrid1.DataBind()myda.Update(myds, “book11”)End SubButton_click事件: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim i As IntegerDim x As StringDim item As DataGridItemFor i
19、 = 0 To DataGrid1.Items.Count 1 item = DataGrid1.Items(i)Dim bookcheckbox As CheckBox = item.FindControl(“checkbox1”)If bookcheckbox.Checked Then 确认数据是否被点击进行删除x = item.Cells(1).Text x为datagrid第二列的数据的内容deletedataset(x) End IfNext11binddata()Response.Write(“删除成功!”) End SubSub deletedataset(ByVal y As
20、String)Dim con As New SqlConnection(“initial catalog=book;uid=sa;pwd=“)Dim cmd As SqlCommandDim qry As StringCon.Open()qry = “delete book08 where 书号=“ uid=sa;pwd=;database=book“Dim myconn As New SqlConnection(connstr)Dim da As New SqlDataAdapter(sql, myconn)Dim ds As New DataSetda。Fill(ds, “book11“)
21、Dim myrow As DataRowDim dacomm As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(da)If ds.Tables(“book08“).Rows.Count = 1 ThenResponse.Redirect(“new.aspx“)Response.Write(“书号重复,请重新输入“)End Ifmyrow = ds.Tables(“book08“).NewRowmyrow(“书号“) = Trim(TextBox1.Text)myrow(“书名“) = Trim(TextBox2.T
22、ext)myrow(“作者“) = Trim(TextBox3.Text)13myrow(“征定时间“) = Trim(TextBox4.Text)myrow(“数量“) = Trim(TextBox5.Text)ds.Tables(“book08“).Rows.Add(myrow)da.UpdateCommand = dacomm.GetUpdateCommandda.Update(ds, “book11“)Response.Redirect(“book.aspx“)End Sub/修改记录。在登陆系统后点击修改按钮进入修改页面 update.aspx/页面中 textbox 的文本来自与
23、update.aspx 中定义并赋值的 Session 变量。用户通过修改其中的文本值并单击确定完成修改/修改部分代码如下:update.aspx 中的 Page Load 事件Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadIf Not Page.IsPostBack Thenbinddata()End IfEnd Sub用于加载页面及确认是否第一次访问。Update.aspx 中的 binddata 事件private Sub bindd
24、ata()Dim myconnection As New SqlConnection(“initial catalog=book;uid=sa;pwd=“)Const strsql As String = “select * from book08 order by 书号“Dim mycommand As New SqlCommand(strsql, myconnection)Dim myda As New SqlDataAdapterMyda.SelectCommand = mycommandDim myds As New DataSet14Myda.Fill(myds)DataGrid1.
25、DataSource = mydsDataGrid1.DataBind()End Sub用于连接数据库并定义相关操作。Update.aspx 中设置 DataGrid 中的 ItemCommandPrivate Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommandDim b_no As TableCell = e.Item.Cells(1)Dim b_name As
26、 TableCell = e.tem.Cells(2)Dim b_a As TableCell = e.Item.Cells(3)Dim b_b As TableCell = e.tem.Cells(4)Dim b_c As TableCell = e.tem.Cells(5)Session(“a“) = b_no.textSession(“b“) = b_name.textSession(“c“) = b_a.textSession(“d“) = b_b.textSession(“e“) = b_c.textResponse.Redirect(“update1.aspx“)End Sub用于将 Session 变量的值传给 textboxupdate.aspx 中的 Button1 Click 事件private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickResponse.Redirect(“book.aspx“)End Sub15