如何在Visual Basic 2017和SQL Express 2014中连接远程SQL数据库 [英] How to connect remote SQL database in visual basic 2017 and SQL express 2014

查看:63
本文介绍了如何在Visual Basic 2017和SQL Express 2014中连接远程SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的基本Windows窗体应用程序的商店项目,需要使用5个不同的PC,1个用于商店,2个用于仓库,3,4个用于计费会计和管理用途,我正在创建这个应用程序在tcp客户端服务器基础上并用于sql控件类连接SQL数据库,它只能在桌面上正常工作但不能与另一台PC连接

当我尝试远程连接到SQL数据库时它可以不工作需要帮助

我如何远程连接我的数据库



我尝试过:



这是SQL控件



i am working on my shop project of visual basic windows form application which need to uses 5 different PC ,1 for store ,2 for warehouse and 3 ,4,5 for billing accounting and admin use , i am creating this application on tcp client server base and used to sql control class to connect SQL database , it's only works on desktop properly but not connected with another PC
when i am trying to connect remotely to SQL data base it's could not works need help
how i connect remotely with my database

What I have tried:

this is SQL control

Imports System.Data.SqlClient
Public Class Sql_control
    Public DBCon As New SqlConnection("Data Source=DEV-ASUS\SQLEXPRESS,1433;Network Library=DBMSSOCN;Server=DEV-ASUS\SQLEXPRESS;Database=Buss_db;trusted_connection=true;")
    '
    Private DBCmd As SqlCommand

    ' DB DATA
    Public DBDA As SqlDataAdapter
    Public DBDT As DataTable

    ' QUERY PARAMETERS
    Public Params As New List(Of SqlParameter)

    ' QUERY STATISTICS
    Public RecordCount As Integer
    Public Exception As String

    Public Sub New()
    End Sub

    ' ALLOW CONNECTION STRING OVERRIDE
    Public Sub New(ConnectionString As String)
        DBCon = New SqlConnection(ConnectionString)
    End Sub

    ' EXECUTE QUERY SUB
    Public Sub ExecQuery(Query As String)
        ' RESET QUERY STATS
        RecordCount = 0
        Exception = ""

        Try
            DBCon.Open()
            If DBCon.State = ConnectionState.Open Then MsgBox("open")
            ' CREATE DB COMMAND
            DBCmd = New SqlCommand(Query, DBCon)

            ' LOAD PARAMS INTO DB COMMAND
            Params.ForEach(Sub(p) DBCmd.Parameters.Add(p))

            ' CLEAR PARAM LIST
            Params.Clear()

            ' EXECUTE COMMAND & FILL DATASET
            DBDT = New DataTable
            DBDA = New SqlDataAdapter(DBCmd)
            RecordCount = DBDA.Fill(DBDT)
        Catch ex As Exception
            ' CAPTURE ERROR
            Exception = "ExecQuery Error: " & vbNewLine & ex.Message
        Finally
            ' CLOSE CONNECTION
            If DBCon.State = ConnectionState.Open Then DBCon.Close()
        End Try
    End Sub

    ' ADD PARAMS
    Public Sub AddParam(Name As String, Value As Object)
        Dim NewParam As New SqlParameter(Name, Value)
        Params.Add(NewParam)
    End Sub

    ' ERROR CHECKING
    Public Function HasException(Optional Report As Boolean = False) As Boolean
        If String.IsNullOrEmpty(Exception) Then Return False
        If Report = True Then MsgBox(Exception, MsgBoxStyle.Critical, "Exception:")
        Return True
    End Function
End Class







这是我希望与数据库连接的表格1






this is form 1 that i want connect with database

Public Class Form1
    Private sql As New Sql_control
    Private Client As TCPControl

    Dim yv_msg As Integer = 1
    Private Sub cmdSend_Click(sender As System.Object, e As System.EventArgs) Handles cmdSend.Click
        SendMessage()
        txtMessage.Clear()

    End Sub

    Private Sub cmdConnect_Click(sender As System.Object, e As System.EventArgs) Handles cmdConnect.Click
        Client = New TCPControl("192.168.43.239", 8888)
        If Client.Client.Connected Then cmdConnect.Text = "Connected"
        TxtMessage.Text = yv_msg & Space(2) & "Data Lode"
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Client.Client.Connected = True Then
            Client.DataStream.Close()
            Client.Client.Close()
        End If
    End Sub

    Private Sub txtMessage_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMessage.KeyDown
        If e.KeyCode = Keys.Enter Then SendMessage()
    End Sub

    Private Sub SendMessage()
        If Client.Client.Connected = True Then
            Client.Send(TxtMessage.Text)

        End If
    End Sub
    Public UpdateText As String = ""
    Private Sub OnLineReceived(sender As TCPControl, Data As String)
        ' UpdateText(Data)
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Txtuserid_KeyDown(sender As Object, e As KeyEventArgs) Handles Txtuserid.KeyDown
        If e.KeyCode = Keys.Enter Then
            If String.IsNullOrEmpty(Txtuserid.Text) Then
                MsgBox("Invalid Input ")
                Txtuserid.Clear()
                Txtuserid.Select()
            End If
            If Not String.IsNullOrEmpty(Txtuserid.Text) Then
                TxtPAssword.Select()
            End If
        End If
    End Sub

    Private Sub TxtPAssword_KeyDown(sender As Object, e As KeyEventArgs) Handles TxtPAssword.KeyDown
        If e.KeyCode = Keys.Enter Then
            If String.IsNullOrEmpty(TxtPAssword.Text) Then
                MsgBox("Invalid Input ")
                TxtPAssword.Clear()
                TxtPAssword.Select()
            End If
            If Not String.IsNullOrEmpty(TxtPAssword.Text) Then
                Cmd_log.Select()
            End If
        End If
    End Sub
    Dim m_name As String = ""
    Dim L_name As String = ""
    Private Sub Cmd_log_Click(sender As Object, e As EventArgs) Handles Cmd_log.Click

        sql.AddParam("@a1", Txtuserid.Text)
        sql.AddParam("@a2", TxtPAssword.Text)
        sql.ExecQuery("Select username,password,name,lastname from user1 where username=@a1 and password=@a2  ")
        If sql.RecordCount < 1 Then MsgBox("No data found as per your requiest ") : Exit Sub
        Dim r As DataRow = sql.DBDT.Rows(0)
        m_name = r("name").ToString
        L_name = r("lastname").ToString
        MsgBox(" you Log as " & Space(2) & m_name & Space(2) & L_name)

        'username,password,name,lastname     :user1
    End Sub
End Class

推荐答案

有一些步骤你必须经历。详细信息

如何连接到另一台计算机的SQL Server? - 堆栈溢出 [ ^ ]

第2课:从另一台计算机连接| Microsoft Docs [ ^ ]

从另一台计算机连接到数据库引擎 [ ^ ]
There are some steps you have to go through. Details here
How to connect to SQL Server from another computer? - Stack Overflow[^]
Lesson 2: Connecting from Another Computer | Microsoft Docs[^]
Connecting to the Database Engine from Another Computer[^]


我记得,如果您使用的是SQLServer Express,则无法从远程计算机连接到它;它只会从安装的计算机上连接。



所以..如果你想使用SQLServer,你需要使用完整版(不是Express)版本)或在您的主机PC上编写一个应用程序来处理所有数据库请求。
As I recall, if you are using SQLServer Express, you cannot connect to it from a remote computer; it will only connect from the computer it is installed on.

So.. if you want to use SQLServer, you either need to use a full version (not an Express version) or write a application on your host PC that will process all database requests for you.


这篇关于如何在Visual Basic 2017和SQL Express 2014中连接远程SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆