|
|
#2
wasdyjyj2008-05-31 13:04
Imports System.Data
Imports System.Data.SqlClient Public Class check '类名 Private t As New DataTable Public Function checkNULL(ByVal name As String, ByVal password As String) As String If name <> "" And password = "" Then Return "请输入密码!" Exit Function ElseIf name = "" And password <> "" Then Return "请输入账号!" Exit Function ElseIf name = "" And password = "" Then Return "请输入账号与密码!" Exit Function Else check(name, password) End If End Function Public Function check(ByVal name As String, ByVal password As String) As Boolean Dim connectionString As String 'Call DBopen() connectionString = "server=(local);database=Northwind;uid=sa" Dim connector As New SqlConnection connector.ConnectionString = connectionString Dim commander As New SqlCommand = "SELECT name FROM users WHERE name=@name AND password=@password" = CommandType.Text commander.Connection = connector Dim parPassword As New SqlParameter parPassword.ParameterName = "@password" parPassword.SqlDbType = SqlDbType.Char parPassword.Size = 20 parPassword.Value = password parPassword.Direction = ParameterDirection.Input Dim parName As New SqlParameter parName.ParameterName = "@name" parName.SqlDbType = SqlDbType.Char parName.Size = 20 parName.Value = name parName.Direction = ParameterDirection.Input commander.Parameters.Add(parName) commander.Parameters.Add(parPassword) Try connector.Open() Dim reader As SqlDataReader = commander.ExecuteReader(CommandBehavior.CloseConnection) If reader.HasRows Then Return True '登陆成功 End If reader.Close() Catch ex As Exception Return False '登陆失败 Finally If connector.State = ConnectionState.Open Then connector.Close() End If If Not (connector Is Nothing) Then connector.Dispose() End If If commander.Parameters.Count > 0 Then commander.Parameters.Clear() End If If Not (commander Is Nothing) Then commander.Dispose() End If End Try End Function End Class 这个是改的类,没调试,至于空验证最好不要封装,放在按钮时间里面做最好,因为你要求一起封装,所以就写上来了,如果放在按钮时间里,直接调用check接口可以了,你拿去试下 调用时先引用类,实例化后直接掉接口,传参数就可以了 不明白的在问我[qq]276269534[/qq] |
以下是一个登入界面的代码,请问那个高手可以帮我把下面得代码改成用一个类分装,然后用一个摁钮来调用~~~
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Public t As New DataTable
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString As String
If txtName.Text <> "" And txtPassword.Text = "" Then
MsgBox("请输入密码!", , "提示")
ElseIf txtName.Text = "" And txtPassword.Text <> "" Then
MsgBox("请输入帐号!", , "提示")
ElseIf txtName.Text = "" And txtPassword.Text = "" Then
MsgBox("请输入帐号和密码!", , "提示")
Else
'Call DBopen()
connectionString = "server=(local);database=Northwind;uid=sa"
Dim connector As New SqlConnection
connector.ConnectionString = connectionString
Dim commander As New SqlCommand
= "SELECT name FROM users WHERE name=@name AND password=@password"
= CommandType.Text
commander.Connection = connector
Dim parPassword As New SqlParameter
parPassword.ParameterName = "@password"
parPassword.SqlDbType = SqlDbType.Char
parPassword.Size = 20
parPassword.Value = Me.txtPassword.Text
parPassword.Direction = ParameterDirection.Input
Dim parName As New SqlParameter
parName.ParameterName = "@name"
parName.SqlDbType = SqlDbType.Char
parName.Size = 20
parName.Value = Me.txtName.Text
parName.Direction = ParameterDirection.Input
commander.Parameters.Add(parName)
commander.Parameters.Add(parPassword)
Try
connector.Open()
Dim reader As SqlDataReader = commander.ExecuteReader(CommandBehavior.CloseConnection)
If reader.HasRows Then
MessageBox.Show("登入成功!")
Dim f As New Form3
f.Show()
End If
reader.Close()
Catch ex As Exception
MessageBox.Show("登入失败!")
Finally
If connector.State = ConnectionState.Open Then
connector.Close()
End If
If Not (connector Is Nothing) Then
connector.Dispose()
End If
If commander.Parameters.Count > 0 Then
commander.Parameters.Clear()
End If
If Not (commander Is Nothing) Then
commander.Dispose()
End If
End Try
End If