gorm连接mssql

package Dal

import (
	"github.com/jinzhu/gorm"
	"fmt"
	_ "github.com/jinzhu/gorm/dialects/mssql"
	"flag"
)

type GormUtils struct {
	Database *gorm.DB
}

func (o *GormUtils) Connect(){
	flag.Parse()
	//db, err := gorm.Open("mssql", "sqlserver://sa:xxxxxx@localhost:1433?database=Zorro")
	db, err := gorm.Open("adodb","Provider=SQLOLEDB;Data Source=localhost;integrated security=SSPI;Initial Catalog=DBNAME;user id=sa;password=xxxxxx")
	if err != nil{
		fmt.Println(err)
		panic("connection error")

	}
	o.Database = db
	//禁止表名复数形式
	db.SingularTable(true)
}

func GormTest(){
	orm := GormUtils{}
	orm.Connect()
    hasTable := orm.Database.HasTable(&ConfRole{})
    fmt.Println(hasTable)
    var rs []ConfRole
	orm.Database.Find(&rs)
	fmt.Println(rs)
}

type ConfRole struct {
	RoleId int `gorm:"primary_key;column:ROLEID"`
	RoleName string `gorm:"type:nvarchar(50);column:ROLENAME"`
	Description string `gorm:"type:nvarchar(100);column:DESCRIPTION"`
	Order int `gorm:"column:ORDER"`
}

func (o *ConfRole) TableName() string{
	return "ROLES";
}

官方demo给出的连接方式无法连接成功,用ado可行。

你可能感兴趣的:(go)