Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to SQL Server using Windows Auth?

Tags:

sql-server

go

I am currently using this example to connect to SQL Server using Go:

Create Go apps

Here is the example I am using:

    package main

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

    // Replace with your own connection parameters
    var server = "localhost"
    var port = 1433
    var user = "sa"
    var password = "your_password"

    var db *sql.DB

    func main() {
        var err error

        // Create connection string
        connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d",
            server, user, password, port)

        // Create connection pool
        db, err = sql.Open("sqlserver", connString)
        if err != nil {
            log.Fatal("Error creating connection pool: " + err.Error())
        }
        log.Printf("Connected!\n")

        // Close the database connection pool after program executes
        defer db.Close()

        SelectVersion()
    }

Is there any known way to use Windows Authentication to connect to SQL Server? I have tried adding "Trusted_Connection=yes" and removing the username/password.

I have Googled around but have not found any Go Packages that have this option.

like image 916
Kade Williams Avatar asked Jan 17 '18 21:01

Kade Williams


1 Answers

As I posted earlier...

"trusted_connection=yes" did work. I just entered it wrong.

like image 146
Kade Williams Avatar answered Sep 21 '22 07:09

Kade Williams