2012-10-05 73 views
0

我正在尝试创建登录机制,通过它可以访问专用网络中的远程SQL Server 2008 R2数据库(如果凭据正确)。我打算在我的工作服务器上执行数据库,并在与服务器位于同一子网的客户机上执行此程序。这是我到目前为止的代码:使用AutoIT连接到远程SQL Server的问题

Func Login() 

    $loginGUI = GUICreate("Login", 250, 150, -1, -1) ; will create a dialog box that when displayed is centered 
    GUISetState(@SW_SHOW) 
    GUICtrlCreateLabel ("Input valid credentials to login", 40, 10,-1,-1) 
    GUICtrlCreateLabel ("Username", 40, 40,-1,-1) 
    Local $userInput = GUICtrlCreateInput("", 100, 37, 100, 20) 
    GUICtrlSetState($userInput, $GUI_FOCUS) 
    GUICtrlCreateLabel ("Password", 40, 70,-1,-1) 
    Local $passwordInput = GUICtrlCreateInput("", 100, 67, 100, 20, $ES_PASSWORD) 
    Local $loginButton = GUICtrlCreateButton("Login", 40, 105, 70) 
    Local $exitButton = GUICtrlCreateButton("Exit", 130, 105, 70) 

    GUISetState() 
    ; Run the GUI until the dialog is closed 
    While 1 
     $msg = GUIGetMsg(1) 
     Switch $msg[0] 
     Case $loginButton 
      $user = GUICtrlRead($userInput) 
      $password = GUICtrlRead($passwordInput) 
      Global $loginCon = ObjCreate("ADODB.Connection") 
      With $loginCon 
       .ConnectionString =("DRIVER={SQL Server};SERVER=192.168.1.30\SQLEXPRESS;DATABASE=Test;UID="&$user&";PWD="&$password&";") 
       .Open 
      EndWith 
      If ($user ="" and $password="") or @error Then 
       MsgBox(48, "Login error", "Connection failed! Wrong Username/Password.") 
       GUICtrlSetData($userInput, "") 
       GUICtrlSetData($passwordInput, "") 
       GUICtrlSetState($userInput, $GUI_FOCUS) 
      Else 
       $loginCon.Close 
       GUIDelete() 
       Main() 
       ExitLoop 
      EndIf 
     Case $exitButton 
      GUIDelete() 
      ExitLoop 
     Case $GUI_EVENT_CLOSE 
      GUIDelete() 
      ExitLoop 
     EndSwitch 
    WEnd 

EndFunc 

我也做了以下措施:

  1. 使用SQL Server Management Studio中允许远程连接以及正确的用户访问正确的权限数据库。
  2. 使用SQL Server配置管理器启用SQL Server Browser和具有正确配置的TCP/IP协议来访问服务器。
  3. 创建防火墙入站和出站规则,允许服务器和客户端访问TCP端口1433和UDP端口1434(以防万一)。
  4. sqlservr.exesqlbrowser.exe添加到防火墙允许的程序列表中。
  5. 在客户端PC上安装SQL Server Native Client 2008 R2。

我可以使用服务器IP本地连接到我的数据库,但我得到试图从远程客户端连接到服务器时出现以下错误:

err.description是:[微软] [ODBC SQL Server驱动程序] [DBNETLIB] SQL Server不存在或访问被拒绝。

奇怪的是,我可以使用sqlcmd远程客户端连接。我还必须提及,我目前正在使用我的笔记本电脑来保存我的测试数据库。它的IP由DCHP服务器从工作中分配并始终保持不变。

我的代码是否有误或是否必须进行其他服务器/客户端配置?

回答

0

而不是这样做,我建议客户端和服务器写在自动。将服务器放到远程计算机上,并使其与本地数据库交互。比你想要的客户还要多。这种方法也更安全。 Here is where you can start