ms_two_db.go 1.7KB

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 dataBase2 *gorm.DB
  25. var (
  26. dbName2 = "local"
  27. user2 = beego.AppConfig.DefaultString(dbName2+"::user", "KingMed")
  28. password2 = beego.AppConfig.DefaultString(dbName2+"::password", "KingMed")
  29. host2 = beego.AppConfig.DefaultString(dbName2+"::host", "localhost")
  30. port2 = beego.AppConfig.DefaultString(dbName2+"::port", "1433")
  31. dbname2 = beego.AppConfig.DefaultString(dbName2+"::dbname", "KMDB")
  32. )
  33. func ConnectMSDB2() {
  34. connectionString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%s;database=%s;encrypt=disable", host2, user2, password2, port2, dbname2)
  35. fmt.Println(connectionString)
  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. }