Message "Invalid Object name 'dbo.table' SQL Server with Go Lang

Asked

Viewed 3,438 times

0

I am trying to use a select in my SQL Server Database that I have in Azure.

I’m using the Driver: https://github.com/denisenkom/go-mssqldb.

However, a message is returned to me:

mssql: Invalid Object name 'dbo.table'.

Below my code, where I’m wrong?

package main

import (
    _ "github.com/denisenkom/go-mssqldb"
    "database/sql"
    "log"
    "fmt"
)

func main() {
    connectionDB() // inicia conexão com o azure
}

func connectionDB() {

    log.Println("Main:")
    log.Println("Opening")

    db, err := sql.Open("mssql", "server=pwbt.database.windows.net;Initial Catalog=dbo;user id=admin-jose;password=password") //deixei a senha errada para postar aqui

    if err != nil {
        log.Println("Open Failed: ", err.Error())
    }

    log.Println("Opened")
    log.Println("Pinging")

    err = db.Ping();
    if err != nil {
        log.Println("Failed to ping: ", err.Error())
    }

    log.Println("Pinged")

    x := 2
    rows, err := db.Query("select Nome from dbo.tbPessoa where Id=?", x)
    if err != nil {
        log.Fatal(err)
    }

    defer rows.Close()
    defer db.Close()    
    fmt.Println(rows)
    fmt.Printf("bye\n")
}

Return:

C: Go bin go.exe run B: go gocode src main connect-bd.go
2017/02/25 15:59:33 Main:
2017/02/25 15:59:33 Opening
2017/02/25 15:59:33 Opened
2017/02/25 15:59:33 Pinging
2017/02/25 15:59:33 Pinged
2017/02/25 15:59:33 mssql: invalid Object name 'dbo.tbPessoa'.
Exit status 1

inserir a descrição da imagem aqui

1 answer

5

In my connect string I had to say which bank I will use database=PWBT.

Done this connected in a good!

Without this information Select was being done in the standard SQL "master" database.

Stayed like this:

db, err := sql.Open("mssql", "server=pwbt.database.windows.net;user id=admin-jose;password=password;database=PWBT;port=1433")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.