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

  

go 连接mssql

上一篇:LINUX中安装MYSQL,远程连接mysql权限


下一篇:实践 1-1 JDBC使用详解