1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-24 23:59:47 +02:00
documize/vendor/github.com/denisenkom/go-mssqldb/doc/how-to-use-newconnector.md
2019-06-25 15:37:19 +01:00

1.2 KiB

How to use the Connector object

A Connector holds information in a DSN and is ready to make a new connection at any time. Connector implements the database/sql/driver Connector interface so it can be passed to the database/sql OpenDB function. One property on the Connector is the SessionInitSQL field, which may be used to set any options that cannot be passed through a DSN string.

To use the Connector type, first you need to import the sql and go-mssqldb packages

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

Now you can create a Connector object by calling NewConnector, which creates a new connector from a DSN.

dsn := "sqlserver://username:password@hostname/instance?database=databasename"
connector, err := mssql.NewConnector(dsn)

You can set connector.SessionInitSQL for any options that cannot be passed through in the dsn string.

connector.SessionInitSQL = "SET ANSI_NULLS ON"

Open a database by passing connector to sql.OpenDB.

db := sql.OpenDB(connector)

The returned DB maintains its own pool of idle connections. Now you can use the sql.DB object for querying and executing queries.

Example

NewConnector example