go-- 用go-mssql驱动连接sqlserver数据库

import _ "github.com/denisenkom/go-mssqldb"
import (
"crypto/cipher"
"crypto/des"
"crypto/md5"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
_ "odbc/driver"
"strconv"
"bytes"
"flag"
"log"
) var debug = flag.Bool("debug", true, "enable debugging")
var password = flag.String("password", "a*c23**", "the database password")
var port *int = flag.Int("port", , "the database port")
var server = flag.String("server", "123.***.120.**", "the database server")
var user = flag.String("user", "sa", "the database user")
var database = flag.String("database", "Deer_JNCB_********", "the database name") func GetDB() (*sql.DB, error){
if *debug {
fmt.Printf(" password:%s\n", *password)
fmt.Printf(" port:%d\n", *port)
fmt.Printf(" server:%s\n", *server)
fmt.Printf(" user:%s\n", *user)
}
connString := fmt.Sprintf("server=%s;database=%s;user id=%s;password=%s;port=%d;encrypt=disable", *server, *database, *user, *password, *port)
if *debug {
fmt.Printf(" connString:%s\n", connString)
}
db, err := sql.Open("mssql", connString)
if err != nil {
log.Fatal("Open connection failed:", err.Error())
return nil, err
}
err = db.Ping()
if err != nil {
fmt.Print("PING:%s",err)
return nil, err
}
return db, nil
}
encrypt=disable  这个很重要,说三遍.....
上一篇:Java面向对象的三大特性之一 多态


下一篇:Atitti.dw cc 2015 绿色版本安装总结