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", 6003, "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 }
上一篇:英语 下一篇:英语基础词汇
上传者:fcdesk6
返回

有关《go-- 用go-mssql驱动连接sqlserver数据库》的评论