| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 821 人关注过本帖
标题:请高手帮助VB代码改写
只看楼主 加入收藏
youshuling520
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-5-25
收藏
 问题点数:0 回复次数:5 
请高手帮助VB代码改写
以下是一个登入界面的代码,请问那个高手可以帮我把下面得代码改成用一个类分装,然后用一个摁钮来调用~~~
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
搜索更多相关主题的帖子: 代码 
2008-05-31 08:35
wasdyjyj
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-5-8
收藏
得分:0 
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接口可以了,你拿去试下
调用时先引用类,实例化后直接掉接口,传参数就可以了  不明白的在问我
2008-05-31 13:04
youshuling520
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-5-25
收藏
得分:0 
你的QQ是???
2008-05-31 13:24
wasdyjyj
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-5-8
收藏
得分:0 
276269534
2008-05-31 13:28
youshuling520
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-5-25
收藏
得分:0 
我的意思的把和数据库那部分的代码封装起来
2008-05-31 13:34
trg
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-5-11
收藏
得分:0 
tencent://message/?uin=76235593&Site=http://eyp.
2008-06-02 23:43
快速回复:请高手帮助VB代码改写
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024249 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved