duannian3494 2017-12-03 10:33
浏览 116
已采纳

无法连接到MS SQL

I'm playing around with Golang and trying to connect to MS SQL. I'm using github.com/denisenkom/go-mssqldb package and sqlx for this purpose. But I'm getting error: Unable to open tcp connection with host 'localhost:1433': dial tcp [::1]:1433: connectex: No connection could be made because the target machine actively refused it.
I'm completely sure that everything with database itself since it's perfectly working with my.Net project. Here is is the code:

package main

import (
    "database/sql"
    "fmt"
    "log"

    _ "github.com/denisenkom/go-mssqldb"
    "github.com/jmoiron/sqlx"
)

type Excursion struct {
    Id   int            `db:"id"`
    Name sql.NullString `db:"name"`
}

func main() {
    db, err := sqlx.Connect("sqlserver", "server=localhost;user id=DESKTOP-H74S9IT\\andrey.shedko;database=Flex;connection timeout=30;")
    if err := db.Ping(); err != nil {
        log.Fatal(err)
    }
    rows, err := db.Queryx("SELECT Id, Name FROM dbo.Excursions")
    for rows.Next() {
        var item Excursion
        err = rows.StructScan(&item)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf(
            "%d - %s:  %s
===================
",
            item.Id,
            item.Name.String,
        )
    }
    defer db.Close()
}

Could you advice please what is wrong with this?

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题