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 bar:
[Plain]View Plaincopy 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 Plaincopy 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
- }
Using Golang to connect MSSQL under Windows