Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table query always returns an error on mssql2000/2005 #44

Open
myuid opened this issue Mar 30, 2019 · 0 comments
Open

Table query always returns an error on mssql2000/2005 #44

myuid opened this issue Mar 30, 2019 · 0 comments

Comments

@myuid
Copy link

myuid commented Mar 30, 2019

package main

import (
"database/sql"
"flag"
"fmt"
"log"
_ "github.com/mattn/go-adodb"
_ "runtime/cgo"
)

var (
isLocalAuth bool
isMssql2000 bool
remoteServerIP string
instance string
)

func init() {
flag.BoolVar(&isLocalAuth, "local", false, "true windows,false sa.")
flag.BoolVar(&isMssql2000, "mssql2000", false, "is it MSSQL2000?")
flag.StringVar(&remoteServerIP, "ip", "192.168.1.87", "set server remote ip.")
flag.StringVar(&instance, "instance", "MSSQLSERVER", "set instance.")
}

type Mssql struct {
*sql.DB
dataSource string
database string
windows bool
sa *SA
}

type SA struct {
user string
password string
port int
}

func NewMssql() *Mssql {
mssql := new(Mssql)
source := "localhost"
if !isLocalAuth {
if instance == "MSSQLSERVER" {
source = fmt.Sprintf("%s", remoteServerIP)
} else {
source = fmt.Sprintf("%s\%s", remoteServerIP, instance)
}
}
mssql = &Mssql{
dataSource: source,
database: "suilang",
windows: isLocalAuth,
sa: &SA {
user: "sa",
password: "lcs6718",
port: 1433,
},
}

return mssql

}

func (ms *Mssql) Open() error {
config := fmt.Sprintf("Provider=SQLOLEDB;Initial Catalog=%s;Data Source=%s", ms.database, ms.dataSource)
if ms.windows {
config = fmt.Sprintf("%s;Integrated Security=SSPI", config)
} else {
if isMssql2000 {
config = fmt.Sprintf("%s,%d;user id=%s;password=%s", config, ms.sa.port, ms.sa.user, ms.sa.password)
} else {
config = fmt.Sprintf("%s;user id=%s;password=%s", config, ms.sa.user, ms.sa.password)
}
}

var err error
ms.DB, err = sql.Open("adodb", config)
fmt.Println(config)
return err

}

func (ms *Mssql) Select() {
rows, err := ms.Query("select * from users")
if err != nil {
fmt.Println("select error:")
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var uid, name string
if err := rows.Scan(&uid, &name); err != nil {
log.Fatal(err)
fmt.Println("columns error.")
}
log.Printf("uid %s name is %s\n", uid, name)
}
}

func main() {
flag.Parse()
mssql := NewMssql()
defer mssql.Close()
err := mssql.Open()
if err != nil {
fmt.Println("sql open error: ", err)
return
}
mssql.Select()
}

Running the above code on mssql2000 and 2005 always returns an error when querying data, just like
select error:
2019/03/30 22:48:09
exit status 1

go version go1.11.5 linux/amd64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant