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条回答 默认 最新

报告相同问题?