需要引用包:github.com/denisenkom/go-mssqldb
使用 cmd 执行:
go get github.com/denisenkom/go-mssqldb
连接sqlserver的简单代码示例:
func SqlServerTest(ctx context.Context, connString string) bool {
//connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%s", server, userId, paasword, port)
conn, err := sql.Open("mssql", connString)
if err != nil {
log.Fatal("Open connection failed:", err.Error())
return false
}
defer conn.Close()
stmt, err := conn.Exec("select 1")
if err != nil {
log.Fatal("exec failed:", err.Error())
return false
}
affected, err := stmt.RowsAffected()
if affected > 0 {
fmt.Printf("ok")
return true
}
return false
}