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

通过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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)