package main import ( "database/sql" "fmt" "github.com/golang/glog" _ "github.com/minus5/gofreetds" "os" ) //Example how to use gofreetds as mssql driver for standard sql interface. //More information on how to use sql interface: // http://golang.org/pkg/database/sql/ // https://code.google.com/p/go-wiki/wiki/SQLInterface func main() { //get connection string connStr := os.Getenv("GOFREETDS_CONN_STR") if connStr == "" { panic("Set connection string for the pubs database in GOFREETDS_CONN_STR environment variable!\n") } //get db connection db, err := sql.Open("mssql", connStr) if err != nil { panic(err) } defer db.Close() _, err = db.Exec("CREATE TABLE [testtable] ([uuid] [varchar](64) NOT NULL, [bytefield] [varbinary](MAX), CONSTRAINT [PK_METAVISORREGIONAMISV1] PRIMARY KEY ([uuid]))") if err != nil { glog.Error("Error creating table:", err) return } defer db.Exec("DROP TABLE [testtable]") _, err = db.Exec("INSERT INTO testtable (uuid, bytefield) VALUES (?, ?)", "abcde", []byte{1, 2, 3, 4, 5}) if err != nil { glog.Error("Error inserting row:", err) return } _, err = db.Exec("INSERT INTO testtable (uuid, bytefield) VALUES (?, ?)", "fghij", []byte{6, 7, 8, 9, 10}) if err != nil { glog.Error("Error inserting row:", err) return } var uuid string var bytefield []byte rows, err := db.Query("SELECT uuid, bytefield FROM testtable") if err != nil { glog.Error("Error getting rows:", err) return } for rows.Next() { err = rows.Scan(&uuid, &bytefield) fmt.Println(uuid, bytefield) } }