注册 登录
编程论坛 VB.NET论坛

新人一个,请求帮忙,有关链接数据库的问题。

屎宝宝 发布于 2012-11-17 23:43, 718 次点击
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim conn As New SqlClient.SqlConnection
        conn.ConnectionString = "server=DELL PC;uid=sa;pwd=sql;database=gg_user   "
        conn.Open()
        Dim cmd As New SqlClient.SqlCommand
        cmd.Connection = conn
         = "select * from login where user_id='" & Me.txtuser_id.Text & "' and user_pwd=" & Me.txtpassword.Text & " "
        Dim dr As SqlClient.SqlDataReader
        dr = cmd.ExecuteReader
        If dr.Read Then
            If dr!user_state = "停用" Then
                MsgBox("用户已停用!!!")
            End If
            If txtuser_id.Text = dr.Item("user_id") And txtpassword.Text = dr.Item("user_pwd") Then
                MsgBox("登陆成功!")
                Me.Hide()
                Dim form2 As New Form2
                form2.Show()
            End If

        Else
            MsgBox("请输入正确的用户名和密码或用户不存在!!", MsgBoxStyle.Critical)
        End If
        dr.Close()
        conn.Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub txtname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtuser_id.KeyPress
        If e.KeyChar = Chr(13) Then
            txtpassword.Focus()
            txtpassword.SelectAll()
        End If
    End Sub

    Private Sub txtpassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtpassword.KeyPress
        If e.KeyChar = Chr(13) Then
            Button1.Focus()
        End If
    End Sub
End Class
5 回复
#2
屎宝宝2012-11-18 17:18
为什么链接不到数据库啊???啥原因啊???
#3
jianjunfeng2012-11-20 09:03
conn.ConnectionString = "server=DELL PC;uid=sa;pwd=sql;database=gg_user   "
应为:conn.ConnectionString = ""data source=DELL PC;initial catalog=gg_user;user id=sa;password=sql;


试试看看
#4
jianjunfeng2012-11-20 09:05
回复 2楼 屎宝宝
conn.ConnectionString = "data source=DELL PC;initial catalog=gg_user;user id=sa;password=sql;"
"
#5
屎宝宝2012-11-20 11:03
回复 4楼 jianjunfeng
谢了
#6
K2192012-11-21 10:21
信任学习  强烈参观
1