This article describes how the go language accesses a SQL Server database through ODBC. Share to everyone for your reference. Specifically as follows:
Here you need to use the GO-ODBC library, the Open source address is: HTTPS://GITHUB.COM/WEIGJ/GO-ODBC
Copy Code code as follows:
Package main;
Import (
"FMT"
"Database/sql"
_ "Odbc/driver"
)
Func Main () {
Conn,err: = sql. Open ("ODBC", "Driver={sql Server}"; server=192.168.0.7; Uid=sa; PWD=SA123456;DATABASE=ABDB1 ");
if (Err!=nil) {
Fmt. PRINTLN ("Connecting Error");
Return
}
Defer Conn. Close ();
Stmt,err: = conn. Prepare ("Select Top 5 IDs from ab_contents");
if (Err!=nil) {
Fmt. Println ("Query Error", err);
Return
}
Defer stmt. Close ();
Row,err: = stmt. Query ();
If Err!=nil {
Fmt. Println ("Query Error", err);
Return
}
Defer row. Close ();
For row. Next () {
var id int;
If err: = row. Scan (&id); Err==nil {
Fmt. PRINTLN (ID);
}
}
Fmt. Printf ("%s\n", "Finish");
Return
}
I hope this article will help you with your go language program.