VB.NET连接sql server数据库

   之前没有学过vb语言,这两天接手了一个vb的项目,最开始做的当然就是连接数据库了,可是我在网上找各种资料就是找不到一个能解决我问题的连办法,连接数据库的基本原理都知道。用给的几个接口,写几条语句就完了。可是找到的资料真是五花八门,为了不让你们重复我的遭遇,也为了让我自己记住这个连接方法。我就在CSDn上发了,

 Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If txtID.Text = "" Then
            MsgBox("ログインID は空に")
            txtID.Text = ""
            Me.txtID.Focus()
            Exit Sub
        End If
        If Me.txtPassword.Text = "" Then
            MsgBox("パスワード は空に")
            txtPassword.Text = ""
            Me.txtID.Focus()
            Exit Sub
        End If
        If checkLoginAuth(txtID.Text, txtPassword.Text) = True Then
            MsgBox("登陆成功")
            frmC02ReportMenu.Show()
            Me.Hide()
        Else
            MsgBox("ログインIやパスワードエラー")
            txtID.Text = ""
            txtPassword.Text = ""
            Exit Sub
        End If
        ' If txtID.Text = "1" Then
        'frmC02ReportMenu.Show()
        ' Me.Hide()
        '  ElseIf txtID.Text = "2" Then
        '  frmC03CustomerList.Show()
        '  Me.Hide()
        '  End If


        'frm01_CustomerList.Show()
    End Sub

checkLoginAuth(txtID.Text, txtPassword.Text) = True这条语句是调用的开始,其他的语句都是一些判空,合法性之类的

而这个函数放在一个Model块里

Imports System.Data.SqlClient

Module DB
    Public Const C_SQLServerConnectString = "Data Source=192.168.2.104; database=EASY; User Id=kohaisou1; Password=kohaisou1;"
    Public Function checkLoginAuth(ByVal argStaffCode As String, ByVal argPassword As String) As Boolean
        checkLoginAuth = False
        Dim m_DBConnection As New SqlClient.SqlConnection(C_SQLServerConnectString)
        Try
            m_DBConnection.Open()
            Dim m_sqlCommand As SqlClient.SqlCommand = m_DBConnection.CreateCommand()
            m_sqlCommand.CommandText = "SELECT * FROM use_login  WHERE use_id = " & argStaffCode & " AND password = '" & argPassword & "'"


            Dim m_sqlDataReader As SqlClient.SqlDataReader = m_sqlCommand.ExecuteReader()
            m_sqlCommand.Dispose()


            checkLoginAuth = m_sqlDataReader.HasRows
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Function checkLoginAuth")
        Finally
            m_DBConnection.Close()
        End Try


    End Function
End Module

=============

Imports System.Data.SqlClient

这是引进连接数据库的接口语句

 Public Const C_SQLServerConnectString = "Data Source=192.168.###.###; database=EASY; User Id=use; Password=password;"

这个是最重要的连接语句,

Source是数据库所在的主机IP,database是数据库的名称,user与password分别是数据库的用户和密码。执行完这句话,则真正的连接上了数据库

下面是开始调用的语句

Dim m_DBConnection As New SqlClient.SqlConnection(C_SQLServerConnectString)

Dim是VB的类型定义, m_DBConnection为一种内置接口的类型

m_DBConnection.Open()

这句是打开数据库

Dim m_sqlCommand As SqlClient.SqlCommand = m_DBConnection.CreateCommand()

这句是定义标准的sql语句的类型

 m_sqlCommand.CommandText = "SELECT * FROM use_login  WHERE use_id = " & argStaffCode & " AND password = '" & argPassword & "'"

这一句则是定义真正即将执行的sql语句、

 Dim m_sqlDataReader As SqlClient.SqlDataReader = m_sqlCommand.ExecuteReader()

m_sqlDataReader是得到的结果类型,

m_sqlCommand.Dispose()

这一句真正的上数据库执行了

m_DBConnection.Close()

这是关闭数据库

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值