1、VB用户登录管理系统软件开发环境与工具实验报告实验二实验题目:用户登录及管理系统实验地点:软件实验室一指导老师:王琦实验类型:设计型系 班:计算机科学与技术系1103班 姓 名:秦芳芳(2011100316) 2013年11月22日一实验项目名称:用户登录及管理系统二. 实验目的和任务:1、掌握系统登录界面设计基本流程 2、了解用户管理所要实现的基本功能 3、要求利用三层架构思想机ado.Net相关技术实现程序三. 实验内容及要求:1、能够进行登录验证 2、能够实现对用户的基本管理,管理员能够进行查询添加修改删除 3、利用数据库连接访问数据库,提高安全性四实验步骤:1、class1.vb的代码
2、:Imports System.Data.SqlClientPublic Class Class1 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
3、= & 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
4、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
5、= 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.AppSett
6、ings(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 Fun
7、ction 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() Retur
8、n 1 Catch ex As Exception Return -1 & ex.Message 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
9、set sno = & str2 & where sname = & str1 & Try conn.Open() cmd.ExecuteNonQuery() Return 1 Catch ex As Exception Return -1 & ex.Message 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
10、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.Message End Try conn.Close() End Function Public Function search(ByVal str1 As String) As String Dim conn As New SqlConnect
11、ion 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.Message End Try conn.Close() End FunctionEnd Class2、进入界面功能的代码:Imports System.Data.SqlClientP
12、ublic Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim o As New Class1 Dim 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
13、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.HashPasswordForStoringInConfigFile(strpw
14、d, 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 Class3、用户管理和浏览信息界面的代码: Pu
15、blic Class 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) Hand
16、les 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
17、, ByVal e As System.EventArgs) Handles Timer1.Tick Me.ToolStripStatusLabel1.Text = Now End SubEnd Class4、增,删,改,查功能页面的代码: Public Class 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_Cli
18、ck(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
19、.Object, ByVal e As System.EventArgs) Handles Button4.Click Me.Close() search.Show() End SubEnd Class5、显示信息界面的代码: Public Class 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(ByVa
20、l sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim o As New Class1 Dim ds As New DataSet ds = o.getalldata() Me.DataGridView1.Refresh() Me.DataGridView1.DataSource = ds.Tables(student) End SubEnd Class6、插入信息界面的代码: Public Class ins Private Sub Button1_Click(ByVal sender As System.Ob
21、ject, ByVal e As System.EventArgs) Handles Button1.Click Dim o As New Class1 o.insdata(Me.TextBox1.Text, Me.TextBox2.Text) Me.Close() Form2.Show() End SubEnd Class7、查询信息界面的代码:Public Class search Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Cli
22、ck Dim o As New Class1 Dim 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
23、.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).Va
24、lue.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 SubEnd Class8、修改信息界面的代码:Public Class upda Private Sub Button1_Cl
25、ick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim o As New Class1 o.upd(Me.TextBox1.Text, Me.TextBox2.Text) Me.Close() Form2.Show() End SubEnd Class 9、删除信息界面的代码:Public Class del Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventAr
26、gs) Handles Button1.Click Dim o As New Class1 o.deldata(Me.TextBox1.Text) o.deldata(Me.TextBox1.Text) Me.Close() Form2.Show() End SubEnd Class 五、运行结果及截图:(1)建立数据库(2)运行截图 六、实验总结:通过本次实验,我大致掌握了一个登录系统界面设计的基本流程,同时了解了用户管理所要实现的基本功能,而且能够利用三层架构思想机ado.Net相关技术实现程序,能够进行登录验证,实现了对用户的基本管理,在系统中实现了增删查改的功能,利用数据库连接访问数据库,提高了安全性,为以后的学习打下了很好的基础。