VB中用Winsock控件实现远程数据库查询 贾万兴 2001年 6期 你可能已经习惯了在自己的电脑中查询本机数据库中的信息,但当你身处异地想获得一位客户的详细资料时你该怎么办呢?这时你就要借助远程数据库查询软件才能实现了。下面本文将介绍如何利用Visual Basic来编写远程数据库查询软件。   #1 编程思路:   客户端利用Winsock的SendData方法将待查内容发送到服务器端,服务器端利用data控件对数据库进行查询,最终利用Winsock的SendData 方法将查询结果发送到客户端。   #1 绘制窗体:   一、服务器端(界面如^06020502a^1)   新建一个工程在窗体中加入:   Winsock控件,名称为sckServer,procotol为scktcpprocotol   DATA控件,名称为data1 connect为Access,databasename为D:\shenfen.mdb   shenfen.mdb包含姓名、性别、身份证、民族、地址5个字段其类型均为文本。   TEXTBOX控件,共加入6个TEXTBOX控件名称为TEXT1—TEXT6,其中TEXT2—TEXT6分别与DATA1控件中的各字段相关联   二、客户端(界面如^06020502b^2)   新建一个工程在窗体中加入:   winsocK控件,名称为sckClient,procotol为scktcpprocotol   TEXTBOX控件,共加入两个TEXTBOX控件名称为TEXT1—TEXT2   COMMANDBUTTON控件,名为cmdSendData   #1 程序源码:   1.服务器端   Option Explicit   Private retname As String   Private retsex As String   Private retold As String   Private all As String   Private Sub Command1_Click()   Print all   End Sub   Private Sub Form_Load()   sckServer.LocalPort = 1975 '设置端口号   sckServer.Listen '开始监听1975号端口   '读取当前服务器信息,并显示   MsgBox “服务器的IP地址为:” & sckServer.LocalIP, vbOKOnlx,“提示”   MsgBox “服务器的名称为:” & sckServer.LocalHostName, vbOKOnly,“提示”   End Sub   Private Sub Form_Unload(Cancel As Integer)   If sckServer.State <> sckClosed Then sckServer.Close   '退出时中断连接   End Sub   Private Sub sckServer_ConnectionRequest(ByVal requestID As Long)   '当新的连接请求来到时,检查是否已经与其他计算机建立过连接   '如果已经连接关闭连接   If sckServer.State <> sckClosed Then sckServer.Close   '建立新的连接(如果使用控件数组,则可以在不关闭当前连接的情况下与多台计算机建立连接)   sckServer.Accept requestID   End Sub   Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)   Dim Received As String   Dim jj As String   Dim ww As String   '当有数据来到时将数据保存在Received中,并显示   sckServer.GetData Received, vbString   Text1.Text = Received   ww = Mid(Received, 3)   jj = Mid(Received, 1, 2)   '判断待查字段   If jj = Chr(33) & Chr(33) Then GoTo shefen   Data1.Recordset.FindFirst “姓名 = '” & ww & “'”   GoTo last   shefen:Data1.Recordset.FindFirst “身份证 = '” & ww & “'”   last: retname = Text1.Text   retsex = Text3.Text   retold = Text4.Text   If Data1.Recordset.NoMatch Then   all = “”   Else   all=Text6.Text+Text2.Text+Text3.Text+Text4.Text + Text5.Text   End If   sckServer.SendData all   '将符合条件的纪录的所有字段信息打包发回客户端,字段间可用特殊字符隔开以便客户端处理   End Sub   2.客户端   Option Explicit   Private SendTime As Integer   Private name1 As String   Private sex As String   Private old As String   Private Sub cmdSendData_Click()   Dim jwx As String   name1 = “”   sex = “”   old = “”   If Text1.Text <> “” Then   name1 = Chr(63) & Chr(63) & Text1.Text   '将待查姓名前加入两个Chr(63)使服务器端可判断待查字段为姓名   ElseIf Text2.Text <> “” Then   name1 = Chr(33) & Chr(33) & Text2.Text   '将待查姓名前加入两个Chr(33)使服务器端可判断待查字段为身份证号   Else: MsgBox “输入不得为空”,,“警告”   End If   sckClient.SendData name1   '发送name1   End Sub   Private Sub Form_Load()   '远程主机名可在“控制面板-网络-标识”中查到或输入服务器的IP地址   sckClient.RemoteHost = “jwx”   sckClient.RemotePort = 1975 '设置远程端口与服务器端一致   '开始连接   sckClient.Connect   Do   DoEvents   Loop Until sckClient.State = sckConnected Or _   sckClient.State = sckError   '监测连接状态   If sckClient.State = sckError Then   MsgBox “无法连接到服务器”, 48, “提示”   cmdSendData.Enabled = False   GoTo sj   End If   MsgBox “成功连接到服务器:” & sckClient.RemoteHostIP, vbOKOnly, “提示”   sj:   End Sub   Private Sub Form_Unload(Cancel As Integer)   If sckClient.State <> sckClosed Then sckClient.Close   End Sub   Private Sub sckClient_Close()   '关闭连接   sckClient.Close   cmdSendData.Enabled = False   End Sub   Private Sub sckClient_DataArrival(ByVal bytesTotal As Long)   Dim Received As String   '当有数据来到时将数据保存在Received中,并显示   sckClient.GetData Received, vbString   Text1.Text = Received   cmdSendData.Enabled = False   End Sub   Private Sub Text1_Change()   cmdSendData.Enabled = True   End Sub   Private Sub Text2_Change()   cmdSendData.Enabled = True   End Sub   关于Winsock控件的详细使用方法请参考MSDN,本程序调试环境:Win98第二版,Access 97, Visual Basic6.0企业版。