软件开发环境与工具
实验报告
实验二
实验题目:用户登录及管理系统 实验地点:软件实验室一 指导老师:王琦 实验类型:设计型
系 班:计算机科学与技术系1103班 姓 名:秦芳芳(2011100316)
2013年11月22日
一.实验项目名称:用户登录及管理系统 二.实验目的和任务:
1、掌握系统登录界面设计基本流程
2、了解用户管理所要实现的基本功能
3、要求利用三层架构思想机ado.Net相关技术实现程序
三.实验内容及要求:
1、能够进行登录验证
2、能够实现对用户的基本管理,管理员能够进行查询添加修改删除
3、利用ado.net数据库连接访问数据库,提高安全性
四.实验步骤:
1、cla1.vb的代码:
Imports System.Data.SqlClient Public Cla Cla1
Dim strconn As String = Me.getstrconn() Public Function yanzheng(ByVal str1 As String) As Integer
Dim conn As New SqlConnection conn.ConnectionString = strconn conn.Open() Dim cmd As New SqlCommand
cmd.CommandText = \"select * from student where sname =\'\" & str1 & \"\'\" cmd.Connection = conn \'cmd.CommandType = CommandType.Text
Dim dr As SqlDataReader dr = cmd.ExecuteReader If dr.Read Then dr.Close() conn.Close() Return 1 Else
dr.Close() conn.Close() Return 0 End If
End Function
Public Function pwdyanzheng(ByVal str As String) As SqlDataReader
Dim conn As New SqlConnection conn.ConnectionString = strconn conn.Open()
Dim cmd As New SqlCommand
cmd.CommandText = \"select * from student where sname = \'\" & str & \"\'\" cmd.Connection = conn Dim dr As SqlDataReader = cmd.ExecuteReader Return dr End Function
Public Function getstrconn() As String
Dim str As String str = \"server=\" & System.Configuration.ConfigurationManager.AppSettings(\"server\") & \";\" str &= \"uid=\" & System.Configuration.ConfigurationManager.AppSettings(\"uid\") & \";\" str &= \"pwd=\" & System.Configuration.ConfigurationManager.AppSettings(\"pwd\") & \";\" str &= \"database=\" & System.Configuration.ConfigurationManager.AppSettings(\"database\") Return str End Function
Public Function getalldata() As DataSet
Dim conn As New SqlConnection conn.ConnectionString = strconn conn.Open() Dim da As New SqlDataAdapter(\"select * from student\", conn) Dim ds As New DataSet da.Fill(ds, \"student\") conn.Close() Return ds End Function
Public Function insdata(ByVal str1 As String, ByVal str2 As String) As String
Dim conn As New SqlConnection conn.ConnectionString = strconn Dim cmd As New SqlCommand cmd.Connection = conn cmd.CommandText = \"insert into student(sname,sno) values(\'\" & str1 & \"\',\'\" & str2 & \"\') \"
Try
conn.Open() cmd.ExecuteNonQuery() Return \"1\"
Catch ex As Exception
Return \"-1\" & ex.Meage End Try
conn.Close()
End Function
Public Function upd(ByVal str1 As String, ByVal str2 As String) As String
Dim conn As New SqlConnection conn.ConnectionString = strconn Dim cmd As New SqlCommand cmd.Connection = conn cmd.CommandText = \"update student set sno =\'\" & str2 & \"\'where sname =\'\" & str1 & \"\'\"
Try
conn.Open() cmd.ExecuteNonQuery() Return \"1\"
Catch ex As Exception
Return \"-1\" & ex.Meage End Try
conn.Close() End Function
Public Function deldata(ByVal str As String) As String
Dim conn As New SqlConnection conn.ConnectionString = strconn Dim cmd As New SqlCommand cmd.Connection = conn cmd.CommandText = \"delete from student where sname =\'\" & str & \"\'\"
Try
conn.Open() cmd.ExecuteNonQuery() Return \"1\"
Catch ex As Exception
Return \"-1\" & ex.Meage End Try
conn.Close() End Function
Public Function search(ByVal str1 As String) As String
Dim conn As New SqlConnection conn.ConnectionString = strconn Dim cmd As New SqlCommand cmd.Connection = conn cmd.CommandText = \"select * from student\"
Try
conn.Open() cmd.ExecuteNonQuery() Return \"1\"
Catch ex As Exception
Return \"-1\" & ex.Meage End Try
conn.Close() End Function
End Cla
2、进入界面功能的代码:
Imports System.Data.SqlClient Public Cla Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim o As New Cla
1Dim f As Integer
f = o.yanzheng(Me.TextBox1.Text) If f = 0 Then
MsgBox(\"不存在该用户,请重新输入!\") Me.TextBox1.Text = \"\"
Me.TextBox1.Focus() Else
If f = 1 Then
Dim dr As SqlDataReader
dr = o.pwdyanzheng(Me.TextBox1.Text) dr.Read() If RTrim(dr!sno) Me.TextBox2.Text Then MsgBox(\"密码错误,请重新输入!\") Me.TextBox2.Text = \"\"
Me.TextBox2.Focus() Else
Dim strpwd As String
strpwd = Me.TextBox2.Text strpwd = System.Web.Security.FormsAuthentication.HashPawordForStoringInConfigFile(strpwd, \"md5\") Dim frm As New Form2
frm.loginname = Me.TextBox1.Text frm.logintime = TimeOfDay frm.Show() Me.Hide() End If
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click MyBase.Dispose()
End Sub End Cla
3、用户管理和浏览信息界面的代码: Public Cla Form2
Public loginname As String
Public logintime As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Hide() Form3.Show() End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Hide() Form4.Show() End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.loginstatusname.Text = \"用户名:\" & loginname Me.loginstatustime.Text = \"登录时间:\" & logintime End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Me.ToolStripStatusLabel1.Text = Now End Sub End Cla
4、增,删,改,查功能页面的代码: Public Cla Form3
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Hide() del.Show() End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Hide() ins.Show() End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Hide() upda.Show() End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click Me.Close() search.Show() End Sub End Cla
5、显示信息界面的代码: Public Cla Form4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Close() Form2.Show() End Sub
Private Sub Form4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim o As New Cla
1Dim ds As New DataSet ds = o.getalldata() Me.DataGridView1.Refresh() Me.DataGridView1.DataSource = ds.Tables(\"student\") End Sub End Cla
6、插入信息界面的代码: Public Cla ins
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim o As New Cla1
o.insdata(Me.TextBox1.Text, Me.TextBox2.Text) Me.Close() Form2.Show() End Sub End Cla
7、查询信息界面的代码: Public Cla search
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click Dim o As New Cla
1Dim ds As New DataSet
o.search(Me.TextBox1.Text) ds = o.getalldata() Me.DataGridView1.Refresh() Me.DataGridView1.DataSource = ds.Tables(\"student\") End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.Close() Form2.Show() End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick Me.TextBox1.Text = DataGridView1.CurrentRow.Cells.Item(0).Value.ToString Me.TextBox2.Text = DataGridView1.CurrentRow.Cells.Item(1).Value.ToString Me.TextBox3.Text = DataGridView1.CurrentRow.Cells.Item(2).Value.ToString Me.TextBox4.Text = DataGridView1.CurrentRow.Cells.Item(3).Value.ToString Me.TextBox5.Text = DataGridView1.CurrentRow.Cells.Item(4).Value.ToString End Sub End Cla
8、修改信息界面的代码: Public Cla upda
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim o As New Cla1
o.upd(Me.TextBox1.Text, Me.TextBox2.Text) Me.Close() Form2.Show() End Sub End Cla
9、删除信息界面的代码: Public Cla del
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click Dim o As New Cla1
\'o.deldata(Me.TextBox1.Text) o.deldata(Me.TextBox1.Text) Me.Close() Form2.Show() End Sub
End Cla
五、运行结果及截图:
(1)建立数据库
(2)运行截图
六、实验总结:
通过本次实验,我大致掌握了一个登录系统界面设计的基本流程,同时了解了用户管理所要实现的基本功能,而且能够利用三层架构思想机ado.Net相关技术实现程序,能够进行登录验证,实现了对用户的基本管理,在系统中实现了增删查改的功能,利用ado.net数据库连接访问数据库,提高了安全性,为以后的学习打下了很好的基础。