go 连接mssql

go 连接mssql

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()
}

  

上一篇:mssql 递归


下一篇:MSSQL·大数据量历史数据清理的思路