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 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面