Open myuid opened 5 years ago
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
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, }, }
}
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) } }
}
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