This is a creation in
Article, where the information may have evolved or changed.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Directory (?) [+]
- Install the Microsoft SQL Server Native Client
- Installing Golang's MSSQL Drive
- Write test code
I wipe, recently down dozens of g of data (haha, you understand), are the MSSQL format, need to deal with. Want to use Golang processing, so you need Golang connection MSSQL.
1. Install the Microsoft SQL Server Native Client
Go to the official download, download the address:
[Plain]View Plain
copy print?
- http://www.microsoft.com/zh-CN/download/details.aspx?id=35580
http://www.microsoft.com/zh-CN/download/details.aspx?id=35580
2. Install the Golang MSSQL Drive
Go get CODE.GOOGLE.COM/P/ODBC
3. Write the Test code:
[Plain]View Plain
copy print?
- Package Main
- Import (
- _ "Code.google.com/p/odbc"
- "Database/sql"
- "FMT"
- )
- Func Main () {
- Conn, err: = SQL. Open ("ODBC", "Driver={sql server};server=192.168.1.1;port=1433;uid=sa;pwd=password;database=test")
- If err! = Nil {
- Fmt. PRINTLN ("Connecting Error")
- Return
- }
- Defer Conn. Close ()
- stmt, ERR: = conn. Prepare ("Select Top 5 ID from Users_role")
- 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
- }