Using Golang to connect MSSQL under Windows

Source: Internet
Author: User
Tags microsoft sql server stmt

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Directory (?) [+]

    1. Install the Microsoft SQL Server Native Client
    2. Installing Golang's MSSQL Drive
    3. 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?
    1. 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?
  1. Package Main
  2. Import (
  3. _ "Code.google.com/p/odbc"
  4. "Database/sql"
  5. "FMT"
  6. )
  7. Func Main () {
  8. Conn, err: = SQL. Open ("ODBC", "Driver={sql server};server=192.168.1.1;port=1433;uid=sa;pwd=password;database=test")
  9. If err! = Nil {
  10. Fmt. PRINTLN ("Connecting Error")
  11. Return
  12. }
  13. Defer Conn. Close ()
  14. stmt, ERR: = conn. Prepare ("Select Top 5 ID from Users_role")
  15. If err! = Nil {
  16. Fmt. Println ("Query Error", err)
  17. Return
  18. }
  19. Defer stmt. Close ()
  20. Row, err: = stmt. Query ()
  21. If err! = Nil {
  22. Fmt. Println ("Query Error", err)
  23. Return
  24. }
  25. Defer row. Close ()
  26. For row. Next () {
  27. var ID int
  28. If err: = row. Scan (&id); Err = = Nil {
  29. Fmt. PRINTLN (ID)
  30. }
  31. }
  32. Fmt. Printf ("%s\n", "Finish")
  33. Return
  34. }

Using Golang to connect MSSQL under Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.