连接sqlserver

连接sqlserver

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/denisenkom/go-mssqldb"
)

func initSqlServer() {
	c, err := sql.Open("sqlserver", "sqlserver://user:pwd@ip:port/db")
	if err != nil {
		fmt.Println(err)
		return
	}
	err = c.Ping()
	if err != nil {
		fmt.Println(err)
		return
	}
	var name string
	err = c.QueryRow("select name from user limit 1").Scan(&name)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(name)
}

func main() {
	initSqlServer()
}

posted @ 2022-08-23 11:05  Jeff的技术栈  阅读(23)  评论(0编辑  收藏  举报
回顶部