go 连接mssql

180it 2021-09-10 PM 1392℃ 0条
package main
 
import (
    "database/sql"
    "fmt"
    "log"
 
    _ "github.com/denisenkom/go-mssqldb"
)
 
func main() {
    connectString := "server=127.0.0.1;database=yndb;user id=sa;password=sql;port=8829;encrypt=disable"
    println("Connection string=", connectString)
 
    db, err := sql.Open("mssql", connectString)
    defer db.Close()
    println("Open Error:", err)
    if err != nil {
        log.Fatal(err)
    }
 
    // 执行SQL语句
    rows, err := db.Query("select * from tunit")
    if err != nil {
        fmt.Println("query: ", err)
        return
    }
    for rows.Next() {
        var id string
        var name int
        rows.Scan(&id, &name)
        fmt.Printf("id: %s \t name: %d\n", id, name)
    }
 
    println("closing connection")
    db.Close()
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

go 连接mssql