sql-servergogo-gorm

error failed to initialize database, got error unable to open tcp connection with host 'localhost:1433': dial tcp [::1]:1433: connectex:


My environemnt: Windows 10 x64, go 1.20.1 , Microsoft SQL Server 2014 Express edition.

File vy_sql_server.go

package vy_gen_sql_server

// dsn := "sqlserver://sa:12345678@localhost:1433?database=gorm"
import (
    "gorm.io/driver/sqlserver"
    "gorm.io/gen"
    "gorm.io/gorm"
)

// Dynamic SQL
type Querier interface {
    // SELECT * FROM @@table WHERE name = @name{{if role !=""}} AND role = @role{{end}}
    FilterWithNameAndRole(name, role string) ([]gen.T, error)
}

func Main() {
    g := gen.NewGenerator(gen.Config{
        OutPath: "../query",
        Mode:    gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
    })

    gormdb, _ := gorm.Open(sqlserver.Open("sqlserver://sa:12345678@localhost:1433?database=MISASME2023Sample"))
    g.UseDB(gormdb) // reuse your gorm db

    // Generate basic type-safe DAO API for struct `model.User` following conventions
    //g.ApplyBasic(model.User{})

    // Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.User` and `model.Company`
    //g.ApplyInterface(func(Querier) {}, model.User{}, model.Company{})

    // Generate the code
    g.Execute()
}

Error


2023/03/01 15:24:11 C:/Users/Admin/GolandProjects/vy_learn_gorm_2023_03_01/vy_gen_sql_server/vy_sql_server.go:22
[error] failed to initialize database, got error unable to open tcp connection with host 'localhost:1433': dial tcp [::1]:1433: connectex: No connecti
on could be made because the target machine actively refused it.
2023/03/01 15:24:11 Start generating code.
2023/03/01 15:24:11 Generate code done.   

Process finished with the exit code 0

enter image description here

How to fix it?


Solution

  • enter image description here

        dns := "sqlserver://dovy:FooSampleAha@127.0.0.1:53573?database=FooSample&encrypt=disable&connection+timeout=30"