go 连接sqlserver 调用存储过程

func (u *UserController) Get() {
   db, _ := GetDB()
   defer db.Close()

    var m1="json"
    sql := fmt.Sprintf("SELECT dbo.fn_ChineseToSpell('%s') name;", m1)

    row,err := db.Query(sql)
    if err != nil{
      //fmt.Println(3333)
       fmt.Println(err)
   }else{
      //获取结果集
      var NickName string
      for row.Next() {
         row.Scan(&NickName)
      }

      fmt.Println(NickName)


   }
   //defer db.Close()


}

var debug = flag.Bool("debug", false, "enable debugging")
var password = flag.String("password",beego.AppConfig.String("password") , "the database password")
var port *int = flag.Int("port", 1433, "the database port")
var server = flag.String("server", beego.AppConfig.String("yr_host"), "the database server")
var user = flag.String("user", beego.AppConfig.String("user"), "the database user")
var database  = flag.String("database", beego.AppConfig.String("name"), "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
}

你可能感兴趣的:(go)