Go - local MS SQL
package sqlsvr
import (
"wms.api/model"
"fmt"
"database/sql"
_ "github.com/denisenkom/go-mssqldb"
)
//connect
func ConnectDB() *sql.DB{
//connect
// connStr := fmt.Sprintf("server=%s;usr id=%s;password=%s;port=%s;database=%s;",
connStr := fmt.Sprintf("server=%s;integrated security=SSPI;port=%s;usr id=%s;password=%s;database=%s;encrypt=disable;",
host,port,usr, pwd, dbName)
fmt.Println(connStr)
db, err := sql.Open("mssql",connStr)
model.IfErr(err,"connection:")
// defer connDB.Close()
return db
}
var db = sqlsvr.ConnectDB()
tsql := "SELECT date,name,time FROM dbo.WORK;"
rows, err := db.Query(tsql)
model.IfErr(err,"error read Query:")
defer rows.Close()
var d []model.Punch
for rows.Next(){
rd := model.Punch{}
rows.Scan(&rd.Date, &rd.Name, &rd.Time)
d = append(d,rd)
}
fmt.Println(d)
Comments
Post a Comment