go get github.com/mattn/go-adodb
package test
import (
"database/sql"
"fmt"
"strings"
"testing"
)
func TestSqlServer(t *testing.T) {
var conf []string
var db *sql.DB
var err error
conf = append(conf, "Provider=SQLOLEDB")
conf = append(conf, "Data Source=127.0.0.1\\SQLEXPRESS") // sqlserver IP 和 服务器名称
conf = append(conf, "Initial Catalog=zrx_database") // 数据库名
conf = append(conf, "user id=sa") // 登陆用户名
conf = append(conf, "password=1qaz!QAZ") // 登陆密码
fmt.Println(strings.Join(conf, ";"))
db, err = sql.Open("adodb", strings.Join(conf, ";"))
if err != nil {
fmt.Println("sql open:", err)
return
}
// 执行SQL语句
rows, err := db.Query("select * from test.student2")
if err != nil {
fmt.Println("query: ", err)
return
}
for rows.Next() {
var id int64
var name string
var age int64
rows.Scan(&id, &name, &age)
fmt.Printf("Name: %s \t id: %d age: %d\n", name, id, age)
}
}