1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- // Pipe - A small and beautiful blogging platform written in golang.
- // Copyright (C) 2017-2018, b3log.org
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
-
- package service
-
- import (
- "fmt"
- "github.com/astaxie/beego"
- "github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/mssql" // mssql
- _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
- )
-
- var dataBase *gorm.DB
-
- var (
- dbName = "local"
- user = beego.AppConfig.DefaultString(dbName+"::user", "sa")
- password = beego.AppConfig.DefaultString(dbName+"::password", "hy123456")
- host = beego.AppConfig.DefaultString(dbName+"::host", "192.168.10.10")
- port = beego.AppConfig.DefaultInt(dbName+"::port", 1633)
- dbname = beego.AppConfig.DefaultString(dbName+"::dbname", "hyDB")
- )
-
- func ConnectMSDB() {
- connectionString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s",
- host, user, password, port, dbname)
- fmt.Println(connectionString)
- db, err := gorm.Open("mssql", connectionString)
- if err != nil {
- fmt.Println("failed to connect database")
- fmt.Println(err)
-
- }
- db.LogMode(true)
- db.CommonDB()
- dataBase = db
-
- }
|