denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.81k stars 493 forks source link

Support for odbc dsn keyword in connection string #727

Open christophetrinh opened 2 years ago

christophetrinh commented 2 years ago

Hello,

I'm facing an issue while connecting to a mssql server using ODBC and a DSN (Data source Name).

I have the current odbc.ini file:

[TEST-ODBC-MSSQL1]
Driver=ODBC Driver 17 for SQL Server
Server=test.windows.net # the fqdn

and I'm using this script (for example purpose, i'm setting a fake mssql server test.windows.net) :


func main() {
        // with dsn keyword
    connString := "odbc:dsn=TEST-ODBC-MSSQL1;database=test;user id=my_user;password=***;"

        // with server keyword directly
    // connString := "odbc:server=test.windows.net;database=test;user id=my_user;password=***;"

    if len(os.Args) > 1 {
        connString = os.Args[1]
    }
    db, err := sql.Open("sqlserver", connString)
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    err = execProc(db)
    if err != nil {
        log.Fatalf("execproc error: %v", err)
    }
}

func execProc(db *sql.DB) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()
    rows, err := db.QueryContext(ctx, "TEST_PROC")
    if err != nil {
        log.Printf("error: %v", err)
        return err
    }
    defer rows.Close()
    return err
}

When I'm using the connection string using the DSN keyword, I've the following error:

error: unable to open tcp connection with host 'localhost:1433': dial tcp [::1]:1433: connect: connection refused

whereas using the fqdn directly server=test.windows.net; , everything works as expected.

It seems go-mssqldb doesn't support the DSN keyword in the connection string for odbc driver connection to a mssql According to the documentation, DSN corresponds to

Name of an existing ODBC user or system data source. This keyword overrides any values that might be specified in the Server, Network, and Address keywords.

source

Is it planned to be supported?

Thanks by advance, Chris

shueybubbles commented 2 years ago

AFAIK the odbc: syntax is purely to denote the encoding/style of string, not to imply that it looks up DSNs in the ODBC catalog. There's no linkage between go-mssqldb and ODBC.