VB中用Winsock控件实现远程数据库查询
#1 编程思路:
?牐牽突Ф死肳insock的SendData方法将待查内容发送到服务器端,服务器端利用data控件对数据库进行查询,最终利用Winsock的SendData 方法将查询结果发送到客户端。
#1 绘制窗体:
?牐犚弧⒎衿鞫耍ń缑嫒鏭06020502a^1)??
?牐犘陆ㄒ桓龉こ淘诖疤逯屑尤耄?
?牐燱insock控件,名称为sckServer,procotol为scktcpprocotol
?牐燚ATA控件,名称为data1 connect为Access,databasename为D:\shenfen.mdb
?牐爏henfen.mdb包含姓名、性别、身份证、民族、地址5个字段其类型均为文本。
?牐燭EXTBOX控件,共加入6个TEXTBOX控件名称为TEXT1—TEXT6,其中TEXT2—TEXT6分别与DATA1控件中的各字段相关联
?牐牰⒖突Ф耍ń缑嫒鏭06020502b^2)??
?牐犘陆ㄒ桓龉こ淘诖疤逯屑尤耄?
?牐爓insocK控件,名称为sckClient,procotol为scktcpprocotol
?牐燭EXTBOX控件,共加入两个TEXTBOX控件名称为TEXT1—TEXT2
?牐燙OMMANDBUTTON控件,名为cmdSendData
#1 程序源码:
?牐?1.服务器端
?牐燨ption Explicit
?牐燩rivate retname As String
?牐燩rivate retsex As String
?牐燩rivate retold As String
?牐燩rivate all As String
?牐燩rivate Sub Command1_Click()?煟?
?牐燩rint all
?牐燛nd Sub
?牐燩rivate Sub Form_Load()?煟?
?牐爏ckServer.LocalPort = 1975 '设置端口号
?牐爏ckServer.Listen '开始监听1975号端口
?牐牐Ф寥〉鼻胺衿餍畔ⅲ⑾允?
?牐燤sgBox “服务器的IP地址为:” & sckServer.LocalIP, vbOKOnlx,“提示”
?牐燤sgBox “服务器的名称为:” & sckServer.LocalHostName, vbOKOnly,“提示”
?牐燛nd Sub
?牐燩rivate Sub Form_Unload(Cancel As Integer)??
?牐營f sckServer.State <> sckClosed Then sckServer.Close
?牐牐?退出时中断连接
?牐燛nd Sub
?牐燩rivate Sub sckServer_ConnectionRequest(ByVal requestID As Long)??
?牐牐У毙碌牧忧肭罄吹绞保觳槭欠褚丫肫渌扑慊?立过连接
?牐牐?如果已经连接关闭连接
?牐營f sckServer.State <> sckClosed Then sckServer.Close
?牐牐Ы?立新的连接(如果使用控件数组,则可以在不关闭当前连接的情况下与多台计算机建立连接)??
?牐爏ckServer.Accept requestID
?牐燛nd Sub
?牐燩rivate Sub sckServer_DataArrival(ByVal bytesTotal As Long)
?牐燚im Received As String
?牐燚im jj As String
?牐燚im ww As String
?牐牐У庇惺堇吹绞苯荼4嬖赗eceived中,并显示
?牐爏ckServer.GetData Received, vbString
?牐燭ext1.Text = Received
?牐爓w = Mid(Received, 3)??
?牐爅j = Mid(Received, 1, 2)??
?牐牐?判断待查字段
?牐營f jj = Chr(33) & Chr(33) Then GoTo shefen
?牐燚ata1.Recordset.FindFirst “姓名 = '” & ww & “'”
?牐燝oTo last
?牐爏hefen:Data1.Recordset.FindFirst “身份证 = '” & ww & “'”
?牐爈ast: retname = Text1.Text
?牐爎etsex = Text3.Text
?牐爎etold = Text4.Text
?牐營f Data1.Recordset.NoMatch Then
?牐燼ll = “”
?牐燛lse
?牐燼ll=Text6.Text+Text2.Text+Text3.Text+Text4.Text + Text5.Text
?牐燛nd If
?牐爏ckServer.SendData all
?牐牐Ы咸跫募吐嫉乃凶侄涡畔⒋虬⒒乜突Ф耍侄渭淇捎锰厥庾址艨员憧突Ф舜?理
?牐燛nd Sub
?牐?2.客户端
?牐燨ption Explicit
?牐燩rivate SendTime As Integer
?牐燩rivate name1 As String
?牐燩rivate sex As String
?牐燩rivate old As String
?牐燩rivate Sub cmdSendData_Click()?煟?
?牐燚im jwx As String
?牐爊ame1 = “”
?牐爏ex = “”
?牐爋ld = “”
?牐營f Text1.Text <> “” Then
?牐爊ame1 = Chr(63) & Chr(63) & Text1.Text
?牐牐Ы樾彰凹尤肓礁鯟hr(63)使服务器端可判断待查字段为姓名
?牐燛lseIf Text2.Text <> “” Then
?牐爊ame1 = Chr(33) & Chr(33) & Text2.Text
?牐牐Ы樾彰凹尤肓礁鯟hr(33)使服务器端可判断待查字段为身份证号
?牐燛lse: MsgBox “输入不得为空”,,“警告”
?牐燛nd If
?牐爏ckClient.SendData name1
?牐牐Х⑺蚽ame1
?牐燛nd Sub
?牐燩rivate Sub Form_Load()?煟?
?牐牐г冻讨骰稍凇翱刂泼姘?-网络-标识”中查到或输入服务器的IP地址
?牐爏ckClient.RemoteHost = “jwx”
?牐爏ckClient.RemotePort = 1975 '设置远程端口与服务器端一致
?牐牐Э剂?
?牐爏ckClient.Connect
?牐燚o
?牐燚oEvents
?牐燣oop Until sckClient.State = sckConnected Or _
?牐爏ckClient.State = sckError
?牐牐Ъ嗖饬幼刺?
?牐營f sckClient.State = sckError Then
?牐燤sgBox “无法连接到服务器”, 48, “提示”
?牐燾mdSendData.Enabled = False
?牐燝oTo sj
?牐燛nd If
?牐燤sgBox “成功连接到服务器:” & sckClient.RemoteHostIP, vbOKOnly, “提示”
?牐爏j:??
?牐燛nd Sub
?牐燩rivate Sub Form_Unload(Cancel As Integer)??
?牐營f sckClient.State <> sckClosed Then sckClient.Close
?牐燛nd Sub
?牐燩rivate Sub sckClient_Close()?煟?
?牐牐Ч乇樟?
?牐爏ckClient.Close
?牐燾mdSendData.Enabled = False
?牐燛nd Sub
?牐燩rivate Sub sckClient_DataArrival(ByVal bytesTotal As Long)??
?牐燚im Received As String
?牐牐У庇惺堇吹绞苯荼4嬖赗eceived中,并显示
?牐爏ckClient.GetData Received, vbString
?牐燭ext1.Text = Received
?牐燾mdSendData.Enabled = False
?牐燛nd Sub
?牐燩rivate Sub Text1_Change()?煟?
?牐燾mdSendData.Enabled = True
?牐燛nd Sub
?牐燩rivate Sub Text2_Change()?煟?
?牐燾mdSendData.Enabled = True
?牐燛nd Sub
?牐牴赜赪insock控件的详细使用方法请参考MSDN,本程序调试环境:Win98第二版,Access 97, Visual Basic6.0企业版。