.NET+SQL Server2000图书管理系统 第3页
.NET+SQL Server2000图书管理系统 第3页
六、 代码实现
6.1 “图书管理系统”的功能模块图
系统主界面:
Imports System.Data.SqlClient
Imports System.IO
Public Class searchN
Inherits System.Web.UI.Page
Private connectingstring As String
Private myConn As SqlConnection
Private ds As DataSet
Private myAdapter As SqlDataAdapter
Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
Protected WithEvents radiobutton1 As System.Web.UI.WebControls.RadioButton
Protected WithEvents dropdownlist1 As System.Web.UI.WebControls.DropDownList
Protected WithEvents button1 As System.Web.UI.WebControls.Button
Private myCmd As SqlCommand
Public Sub Open()
myConn.Open()
End Sub
Public Sub Close()
myConn.Close()
End Sub
Public Sub Fill(ByVal sqlstr As String)
myAdapter = New SqlDataAdapter(sqlstr, myConn)
ds = New DataSet
myAdapter.Fill(ds)
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
connectingstring = "data source=(local);Database=library;uid=sa;pwd=123456;"
myConn = New SqlConnection(connectingstring)
If Not IsPostBack Then
If Not Session("UserID") Is Nothing Then
If AllowBooking(Session("UserID")) = True Then
End If
End If
End If
End Sub
'private bind
Public Function GetRowsNum(ByVal sqlstr As String) As Integer
If myConn.State = ConnectionState.Closed Then
myConn.Open()
End If
Fill(sqlstr)
Close()
Return ds.Tables(0).Rows.Count
End Function
Public Function AllowBooking(ByVal UserID As String) As Boolean
Dim sqlstr As String = "select * from UserInfo,RoleInfo where UserInfo.RoleID=RoleInfo.RoleID and AllowBooking=1 and UserID='" + UserID + "'"
If GetRowsNum(sqlstr) = 0 Then
Return False
Else
Return True
End If
End Function
Public Sub BindDBGrd(ByVal sqlstr As String, ByVal myDBGrd As DataGrid)
If myConn.State = ConnectionState.Closed Then
myConn.Open()
End If
Fill(sqlstr)
myDBGrd.DataSource = ds.Tables(0).DefaultView
myDBGrd.DataBind()
End Sub
Public Sub BindDBGrd(ByVal sqlstr As String, ByVal myDBGrd As DataGrid, ByVal SortExp As Object)
If myConn.State = ConnectionState.Closed Then
myConn.Open()
End If
Fill(sqlstr)
Dim dv As DataView = ds.Tables(0).DefaultView
dv.Sort = SortExp
myDBGrd.DataSource = dv
myDBGrd.DataBind()
End Sub
Private Sub BindGrid()
If Not Session("sqlstr") Is Nothing Then
Dim sqlstr As String = CType(Session("sqlstr"), String)
BindDBGrd(sqlstr, ResultGrid)
Session("sqlstr") = sqlstr
End If
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
Dim sign As String = ""
If AnyChoice.Checked = True Then
sign = "%"
End If
Dim sqlstr As String = "select * from BookInfo where 1=1 "
sqlstr += " and " + dropdownlist1.SelectedValue + " like '" + sign + txtContent.Text.ToString.Trim + sign + "' "
Session("sqlstr") = sqlstr
BindGrid()
End Sub
Private Sub ResultGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles ResultGrid.PageIndexChanged
ResultGrid.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub
Private Sub ResultGrid_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles ResultGrid.SortCommand
Dim sqlstr As String
If viewstate("sortexp") Is Nothing Then
viewstate("sortexp") = e.SortExpression.ToString
ElseIf viewstate("sortexp") = e.SortExpression.ToString Then
viewstate("sortexp") += " desc"
Else
viewstate("sortexp") = e.SortExpression.ToString
End If
If Not Session("sqlstr") Is Nothing Then
sqlstr = CType(Session("sqlstr"), String)
BindDBGrd(sqlstr, ResultGrid, Viewstate("sortexp"))
End If
End Sub
Public Sub ExecNonSql(ByVal sqlstr As String)
If myConn.State = ConnectionState.Closed Then
myConn.Open()
End If
myCmd = New SqlCommand(sqlstr, myConn)
myCmd.ExecuteNonQuery()
myCmd.Dispose()
Close()
End Sub
End Class
可以根据书名,内容进行查询,我们选取任意匹配:
如上图所示最上方为自定义用户控件:bar
点击权限维护:
Imports System.Data.SqlClient
Public Class user
Inherits System.Web.UI.Page
Private connectingstring As String
Private myConn As SqlConnection
Private ds As DataSet
Private myAdapter As SqlDataAdapter
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
Protected WithEvents Login_trname As System.Web.UI.HtmlControls.HtmlTableRow
Protected WithEvents Login_trpassword As System.Web.UI.HtmlControls.HtmlTableRow
Private myCmd As SqlCommand
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
connectingstring = "data source=(local);Database=library;uid=sa;pwd=123456;"
myConn = New SqlConnection(connectingstring)
End Sub
Public Function CheckUserberInfo(ByVal Userstr As String, ByVal Pwdstr As String) As Integer
If myConn.State = ConnectionState.Closed Then
myConn.Open()
End If
Dim sqlstr As String
If Pwdstr = "" Then
sqlstr = "select * from manage where manageid='" + Userstr.Trim + "' and Pass is null"
Else
sqlstr = "select * from manage where manageid='" + Userstr.Trim + "' and Pass='" + Pwdstr + "'"
End If
Fill(sqlstr)
If ds.Tables(0).Rows.Count = 0 Then
Close()
Return -1
End If
ds.Clear()
Close()
Return 1
End Function
Public Sub Open()
myConn.Open()
End Sub
Public Sub Close()
myConn.Close()
End Sub
Public Sub Fill(ByVal sqlstr As String)
myAdapter = New SqlDataAdapter(sqlstr, myConn)
ds = New DataSet
myAdapter.Fill(ds)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If CheckUserberInfo(TextBox1.Text, TextBox2.Text) = 1 Then
Session("UserID") = TextBox1.Text.ToString
Response.Redirect("delete.aspx")
Else
Response.Write("<script>javascript:alert('登陆失败!!!');</script>")
Response.Write("javascript:window.location='user.aspx'")
End If
End Sub
End Class