ms_db.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Pipe - A small and beautiful blogging platform written in golang.
  2. // Copyright (C) 2017-2018, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package service
  17. import (
  18. "fmt"
  19. "github.com/astaxie/beego"
  20. "github.com/jinzhu/gorm"
  21. _ "github.com/jinzhu/gorm/dialects/mssql" // mssql
  22. _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
  23. )
  24. var dataBase *gorm.DB
  25. var (
  26. dbName = "local"
  27. user = beego.AppConfig.DefaultString(dbName+"::user", "sa")
  28. password = beego.AppConfig.DefaultString(dbName+"::password", "hy123456")
  29. host = beego.AppConfig.DefaultString(dbName+"::host", "192.168.10.10")
  30. port = beego.AppConfig.DefaultInt(dbName+"::port", 1633)
  31. dbname = beego.AppConfig.DefaultString(dbName+"::dbname", "hyDB")
  32. )
  33. func ConnectMSDB() {
  34. connectionString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s",
  35. host, user, password, port, dbname)
  36. db, err := gorm.Open("mssql", connectionString)
  37. if err != nil {
  38. fmt.Println("failed to connect database")
  39. fmt.Println(err)
  40. }
  41. db.LogMode(true)
  42. db.CommonDB()
  43. dataBase = db
  44. }