any example of how to connect and run a query?
评论:
ackondro:
I'd use https://github.com/denisenkom/go-mssqldb with something like the folowing:
package main import ( "database/sql" "fmt" _ "github.com/denisenkom/go-mssqldb" ) func main() { dsn := "server=localhost;user id=me;password=pass;database=my_db db, err := sql.Open("mssql", dsn) if err != nil { fmt.Println("Cannot connect: ", err.Error()) return } err = db.Ping() if err != nil { fmt.Println("Cannot connect: ", err.Error()) return } defer db.Close() rows, err := db.Query("SELECT users.id FROM users WHERE users.age = ?", 12) if err != nil { fmt.Println("Cannot query: ", err.Error()) return } defer rows.Close() for rows.Next() { var val []interface{} err = rows.Scan(val...) if err != nil { fmt.Println(err) continue } fmt.Println(val) } }