package dbquery import ( "database/sql" "errors" "fmt" "log" "time" _ "github.com/denisenkom/go-mssqldb" ) var MSDB_CONN *sql.DB func MSConnect(DBHOST, DBUSER, DBPWD, DBNAME, DBPORT string, conns ...int) error { log.Println("msdb connectting...") var dbConnErr error if DBHOST != "" && DBUSER != "" && DBPWD != "" && DBPORT != "" { //&& DBNAME != "" for i := 0; i < 10; i++ { //连接字符串 connString := fmt.Sprintf("server=%s;port%d;database=%s;user id=%s;password=%s", DBHOST, DBPORT, DBNAME, DBUSER, DBPWD) log.Println(connString) //建立连接 MSDB_CONN, dbConnErr = sql.Open("mssql", connString) if dbConnErr != nil { log.Println("ERROR", "can not connect to Database, ", dbConnErr) time.Sleep(time.Second * 5) } else { log.Println("msdb connected") break } } } else { return errors.New("msdb connection params errors") } return dbConnErr } func CloseMSConn() error { return MSDB_CONN.Close() }