dongnu4254 2017-03-13 06:06 采纳率: 100%
浏览 827
已采纳

通过go-mssqldb进行golang SQL Server查询

我尝试着查询 SQL Server 2008 R2 使用 go

https://github.com/denisenkom/go-mssqldb.

SQL Server 2008 R2实例位于Windows Server 2008 R2下的虚拟机上; 我正在Win 7 VMWare主机下进行开发,并从那里运行程序以查询VM上的数据库。 数据库已启动并运行服务器虚拟机上托管的应用程序。 代码如下。 我得到的错误是: [EDIT 2017-03-14:指定端口时出现新错误]

Login error: read tcp 192.168.91.1:15222->192.168.91.135:1433: wsarecv: An existing connection was forcibly closed by the remote host.

指定SQL Server端口(1433)时,将返回此错误。 包含或不包含实例都不会更改它。 SQL Server配置为允许远程连接,SQL Server身份验证,未加密的连接,启用TCP / IP,IPALL端口= 1433。 已为80、443、1433、1434上的TCP打开防火墙; UDP,1433,1434年。在将数据库实例添加到连接字符串中之前,我遇到了另一个错误。 SQL Server日志似乎表明机器正在通话。 IP地址用于VMWare主机和VM。 SQL Server浏览器服务正在运行(访问“本地服务”)。 SQL Server代理未运行。 我尝试使用ODBC和ADO连接字符串,但似乎遇到了相同的错误。如有 任何的帮助,我将不胜感激。
package main
import (
   // Import go-mssqldb strictly for side-effects
   _ "github.com/denisenkom/go-mssqldb"
   "database/sql"
   "log"
)

func main() {
   var n_tables int

   println (sql.Drivers())

   // URL connection string formats
   //    sqlserver://sa:mypass@localhost?database=master&connection+timeout=30         // username=sa, password=mypass.
   //    sqlserver://sa:my%7Bpass@somehost?connection+timeout=30                       // password is "my{pass"
   // note: pwd is "myP@55w0rd"
   connectString := "sqlserver://SBM:myP%4055w0rd@VM17:1433?database=AE&connection+timeout=30"
   println("Connection string=" , connectString )

   println("open connection")
   db, err := sql.Open("mssql", connectString)
   defer db.Close()
   println ("Open Error:" , err)
   if err != nil {
      log.Fatal(err)
   }

   println("count records in TS_TABLES & scan")
   err = db.QueryRow("Select count(*) from ts_tables").Scan(&n_tables)
   if err != nil {
      log.Fatal(err)
   }
   println ("count of tables" , n_tables)

   println("closing connection")
   db.Close()
}

output:

[2/2]0xc042002c20
Connection string= sqlserver://VM17_SBM:P%4055word@VM17:1433?database=VM17_SBM_AE_OE_REPO_CL&connection+timeout=30
open connection
Open Error: (0x0,0x0)
count records in TS_TABLES & scan
2017/03/14 19:48:01 Login error: read tcp 192.168.91.1:15222->192.168.91.135:1433: wsarecv: An existing connection was forcibly closed by the remote host.
exit status 1
  • 写回答

2条回答 默认 最新

  • dongyuan2388 2017-03-15 04:12
    关注

    I found the answer in a comment by the library author on Github.

    Adding the "encrypt=disable" to the connection string did it. I'm downloading the SP3 update for SQL Server 2008 R2 x64 as suggested here and will install it when I get some time. As for now though the query works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dq13733519262 2017-03-13 09:37
    关注

    The github repo says below

    Ensure that the SQL Server Browser windows service is running and there is no firewall blocking UDP port 1434. This service is used by the driver to get the TCP port of the SQL Server instance

    Ensure your SQLbrowser service is running on your host..

    also you can specify your connection string along with port number(this negates the need of starting SQLBrowser service,if your port is static)

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 scikit安装之后import不了
  • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊
  • ¥15 人脸识别相关算法 YOLO,AI等
  • ¥15 spark问题方便加V17675797079
  • ¥15 Python代码不打印的原因
  • ¥20 微软SEAL库的安装和使用,VS2022环境配置问题
  • ¥15 数学问题也不知道那种类型的问题
  • ¥15 R作图的时候,文本框重叠了怎么办?
  • ¥15 用python重建流场
  • ¥15 stm32 调试舵机pwm输出异常