mssql.go 683 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package orm
  2. import (
  3. "fmt"
  4. _ "github.com/denisenkom/go-mssqldb"
  5. "github.com/jmoiron/sqlx"
  6. "log"
  7. "lttc-go-adm-znaf/pkg/setting"
  8. )
  9. var (
  10. DB *sqlx.DB
  11. )
  12. // 初始化数据库链接
  13. func Setup() {
  14. var err error
  15. // 拼接数据库链接字符串
  16. dsn := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s",
  17. setting.DatabaseSetting.Server,
  18. setting.DatabaseSetting.UserId,
  19. setting.DatabaseSetting.Password,
  20. setting.DatabaseSetting.Port,
  21. setting.DatabaseSetting.DataBase)
  22. // 链接数据库
  23. DB, err = sqlx.Connect("sqlserver", dsn)
  24. if err != nil {
  25. log.Fatalf("orm.setup err: %v", err)
  26. }
  27. }
  28. // 关闭数据库链接
  29. func Close() {
  30. _ = DB.Close()
  31. }