In order to be able to use Linux under the Golang environment to connect SQL Server 2008 to check a lot of data, eventually using UNIXODBC FreeTDS and other connections successfully.
Run the following shell
#!/bash/bin
sudo apt-get update && sudo apt-get upgrade-y
sudoApt-getInstallUnixodbc unixodbc-bin Unixodbc-dev freetds-bin freetds-dev TDSODBC-yEcho "================================================================="Echo "[FreeTDS]Description = FreeTDS Driver v0. theDriver=/usr/lib/x86_64-linux-gnu/odbc/Libtdsodbc.sosetup=/usr/lib/x86_64-linux-gnu/odbc/Libtdss.sofileusage=1Dontdlclose=1Usagecount=1Client CharSet=utf-8"| sudo tee-a/etc/odbcinst.iniEcho "[TestDB]Driver =freetdsdescription=My Test servertrace=Noservername=Demosql # This corresponds to the name in the following freetds.conf port=1433Database=Db_demo # Modify the name of the database you need to connect to tds_version=7.2"
| sudo tee-a/etc/odbc.iniEcho "[Demosql]Host =192.168.1.1#这里填写自己的数据库地址Port=1433TDS Version=7.2 # Connection sql2008, according to FreeTDS official website, can use version 7.2
#http://www.freetds.org/userguide/choosingtdsprotocol.htm
"| sudo tee-a/etc/freetds/freetds.confEcho "================================================================="Echo "isql-v testdb username passwd"
Connect test with ISQL-V DSN name username password after completion
Download the ODBC package required for go after the test is successful
Go get-v-u github.com/weigj/go-odbc
Next, write a test program:
Package Mainimport ("FMT" "GITHUB.COM/WEIGJ/GO-ODBC") func main () {conn, _: = ODBC. Connect ("DSN=TESTDB; Uid=sa; Pwd=password ") stmt, _: = conn. Prepare ("SELECT top * from Studentinfo") stmt. Execute () rows, _: = stmt. Fetchall () for I, row: = Range rows {fmt. Println (i, Row)}stmt. Close () Conn. Close ()}
Results:
[' Run ' | Done: -. 224378ms]0&{[ -]} 1&{[ at]} 2&{[ the]} 3&{[ at]} 4&{[ -]} 5&{[ -]} 6&{[ +]} 7&{[ the]} 8&{[ *]} 9&{[ -]}
Ubuntu 14.04 ODBC Configuration