Golang Remote connection MSSQL SQL Server

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

SQL Server 2008 Configuring the remote connection method, refer to Baidu Experience: http://jingyan.baidu.com/article/6c67b1d6ca06f02787bb1ed1.html

The Golang connection remote method is as follows:

Package Mainimport ("Database/sql" "FMT" "Strings") Import (_ "Github.com/mattn/go-adodb") type Mssql struct {*sql.  Dbdatasource stringdatabase stringwindows boolsa sa}type SA struct {user stringpasswd stringport Int}func (M *mssql) Open () (err error) {var conf []stringconf = append (conf, "provider=sqloledb") conf = append (conf, "Data source=" +m.datasour CE) if m.windows {///Integrated SECURITY=SSPI This indicates that the current Windows system user is logged in to the SQL Server server (which needs to be set at the time of installation of SQL Servers),//If SQL An error occurs when the server server does not support this way of logging on. conf = append (conf, "Integrated SECURITY=SSPI")}conf = append (conf, "Initial catalog=" +m.database) conf = append (conf, "us Er id= "+m.sa.user) conf = append (conf," password= "+m.sa.passwd) conf = append (conf," port= "+fmt. Sprint (M.sa.port)) m.db, err = sql. Open ("ADODB", strings. Join (conf, ";")) If err! = Nil {return Err}return Nil}func main () {db: = Mssql{datasource: "10.10.2.140\\sqlexpress", Database: "Test",// Windwos:true for Windows authentication, false must set SA account and password windows:false,sa:sa{user: "sa", passwd: "123456 ", port:1433,},}//connection Database err: = db. Open () if err! = Nil {fmt. PRINTLN ("SQL Open:", err) Return}defer db. Close ()//Execute SQL statement rows, err: = db. Query ("SELECT * FROM Info") If err! = Nil {fmt. PRINTLN ("Query:", err) return}for rows. Next () {var name stringvar number introws. Scan (&name, &number) fmt. Printf ("name:%s \ t:%d\n", Name, Number)}}


Related Article

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.