Golang Remote Connection MSSQL 2000

Source: Internet
Author: User
Tags mssql mssqlserver sprintf
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Database/sql" "Flag" "FMT" "Log") Import (_ "Github.com/mattn/go-adodb") Var (local Boolremoteip Stringremoteds string) Func init () {flag. Boolvar (&local, "local", True, "set window Connect.") Flag. Stringvar (&remoteip, "Remoteip", "192.168.1.104", "Set up remote MSSQL of IP.") Flag. Stringvar (&remoteds, "Remoteds", "MSSQLSERVER", "Set up remote mssql of DataSource.")} Type Mssql struct {*sql. Dbdatasource stringdatabase stringwindows boolsa *sa}type SA struct {user stringpasswd stringport Int}fun C Newmssql () *mssql {mssql: = new (MSSQL) DataS: = "localhost" if!local {DataS = fmt. Sprintf ("%s\\%s", Remoteip, remoteds)}mssql = &mssql{//If the database is the default instance (MSSQLSERVER), the IP is used directly, and the named instance needs to be specified. DataSource: "192.168.1.104\\mssqlserver", Datasource:datas,database: "Iman",//Windows:true for Windows authentication, False SA account and password must be set Windows:local,sa: &sa{user: "sa", passwd: "123456", Port:1433,},}return Mssql}func (M *mssql) Open () ER Ror {config: = fmt. Sprintf ("Provider=sqloledb;initial catalog=%s;data source=%s ", M.database, M.datasource) if m.windows {config = fmt. Sprintf ("%s;integrated security=sspi", config)} else {//SQL 2000 has a different port notation than SQL 2005, separated by a comma after data Source. Config = fmt. Sprintf ("%s,%d;user id=%s;password=%s", config, M.sa.port, M.sa.user, m.sa.passwd)}var err Errorm. DB, err = sql. Open ("ADODB", config) fmt. Println (config) return Err}func (M *mssql) Select () {rows, err: = M.query ("Select ID, name from users") if err! = Nil {FMT.P rintf ("Select query err:%s\n", err)}for rows. Next () {var id, name Stringrows. Scan (&id, &name) fmt. PRINTF ("id =%s, name =%s\n", ID, name)}}func main () {flag. Parse () MSSQL: = Newmssql () Err: = MSSQL. Open () checkerror (ERR) MSSQL. Select ()}func checkerror (err error) {if err! = Nil {log. Fatal (ERR)}}

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.