This is a creation in Article, where the information may have evolved or changed.
The go language accesses SQL Server through ODBC, which needs to use the GO-ODBC Library, open Source address:: HTTPS://GITHUB.COM/WEIGJ/GO-ODBC
First, drive installation
Open the Gopath in cmd:
Go to src Directory
In the SRC directory, get the ODBC driver via git:
git clone git: //github.com/weigj/go-odbc.git odbc
Go to the odbc directory:
Run the go install command:
Tip The GCC compiler is not installed to download http://tdm-gcc.tdragon.net/on the official website.
Ii. installation of GCC
or download Mingw-w64-install.exe (170.0 KB), reboot after installation.
is the Mingw-w64-install.exe (https://sourceforge.net/projects/mingw-w64/) installation process:
Configure the GCC environment, open system variables, modify path, add E:\Program files\mingw-w64\x86_64-5.3.0-posix-seh-rt_v4-rev0\mingw64\bin, such as:
Run the Go install command again without prompting for an error message, such as:
Iii. Examples of programs
Idea: Link database--Create a query--display query results
package main import ("database / sql"
"fmt" _ "odbc / driver") func main () {fmt.Printf ("% s \ n", "Create database link") conn, err: = sql.Open ("odbc", "Driver = {SQL Server}; SERVER =. \\ sql2008; UID = sa; PWD=admin@163.com; DATABASE = DTcmsdb4 ") if err! = Nil {fmt.Println (" Link error ") return} defer conn.Close () fmt.Printf ("% s \ n", "Build query") stmt, err: = conn.Prepare ("select id from dt_article") if err! = nil {fmt.Println ("Query exception:", err) return} defer stmt.Close () row, err: = stmt.Query () if err! = nil {fmt.Println ("Query error:", err)} defer row.Close () fmt.Printf ("% s \ n "," Dataset display ") for row.Next () {var id int
if err: = row.Scan (& id); err == nil {fmt.Println (id)}} fmt.Printf ("% s \ n", "Complete query") return}
The problem comes with running successfully through the command line:
Running through the VSC, the following error is indicated:
Analysis reason: In the D:\GoWorks directory my first go.go file and Access-sqlserver.go, both have the main function.
WORKAROUND: Modify the main function name in the My first go.go file to Maina.
On secondary commissioning, success:
As a summary, in VSC, all the files in the same directory are compiled.