zoukankan      html  css  js  c++  java
  • 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
    ", *password)
            fmt.Printf(" port:%d
    ", *port)
            fmt.Printf(" server:%s
    ", *server)
            fmt.Printf(" user:%s
    ", *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
    ", 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  这个很重要,说三遍.....
  • 相关阅读:
    课程作业02
    课后作业01
    大道至简第一章伪代码
    《大道至简》读后感
    Codeforces 959 F. Mahmoud and Ehab and yet another xor task
    Codeforces 992 E. Nastya and King-Shamans
    Codeforces 835 F. Roads in the Kingdom
    Codeforces 980 D. Perfect Groups
    洛谷 P4315 月下“毛景树”
    JDOJ 1234: VIJOS-P1052 高斯消元
  • 原文地址:https://www.cnblogs.com/mafeng/p/6211654.html
Copyright © 2011-2022 走看看