2637309949 0b0b6d8946 init | 3 سال پیش | |
---|---|---|
.. | ||
internal | 3 سال پیش | |
LICENSE.txt | 3 سال پیش | |
README.md | 3 سال پیش | |
appveyor.yml | 3 سال پیش | |
buf.go | 3 سال پیش | |
bulkcopy.go | 3 سال پیش | |
bulkcopy_sql.go | 3 سال پیش | |
convert.go | 3 سال پیش | |
decimal.go | 3 سال پیش | |
doc.go | 3 سال پیش | |
error.go | 3 سال پیش | |
log.go | 3 سال پیش | |
mssql.go | 3 سال پیش | |
mssql_go110.go | 3 سال پیش | |
mssql_go19.go | 3 سال پیش | |
mssql_go19pre.go | 3 سال پیش | |
net.go | 3 سال پیش | |
ntlm.go | 3 سال پیش | |
parser.go | 3 سال پیش | |
rpc.go | 3 سال پیش | |
sspi_windows.go | 3 سال پیش | |
tds.go | 3 سال پیش | |
token.go | 3 سال پیش | |
token_string.go | 3 سال پیش | |
tran.go | 3 سال پیش | |
types.go | 3 سال پیش | |
uniqueidentifier.go | 3 سال پیش |
Requires Go 1.8 or above.
Install with go get github.com/denisenkom/go-mssqldb
.
The recommended connection string uses a URL format:
sqlserver://username:password@host/instance?param1=value¶m2=value
Other supported formats are listed below.
user id
- enter the SQL Server Authentication user id or the Windows Authentication user id in the DOMAIN\User format. On Windows, if user id is empty or missing Single-Sign-On is used.password
database
connection timeout
- in seconds (default is 0 for no timeout), set to 0 for no timeout. Recommended to set to 0 and use context to manage query and connection timeouts.dial timeout
- in seconds (default is 15), set to 0 for no timeoutencrypt
disable
- Data send between client and server is not encrypted.false
- Data sent between client and server is not encrypted beyond the login packet. (Default)true
- Data sent between client and server is encrypted.app name
- The application name (default is go-mssqldb)server
- host or host\instance (default localhost)port
- used only when there is no instance in server (default 1433)keepAlive
- in seconds; 0 to disable (default is 30)failoverpartner
- host or host\instance (default is no partner).failoverport
- used only when there is no instance in failoverpartner (default 1433)packet size
- in bytes; 512 to 32767 (default is 4096)
log
- logging flags (default 0/no logging, 63 for full logging)
TrustServerCertificate
certificate
- The file that contains the public key certificate of the CA that signed the SQL Server certificate. The specified certificate overrides the go platform specific CA certificates.hostNameInCertificate
- Specifies the Common Name (CN) in the server certificate. Default value is the server host.ServerSPN
- The kerberos SPN (Service Principal Name) for the server. Default is MSSQLSvc/host:port.Workstation ID
- The workstation name (default is the host name)ApplicationIntent
- Can be given the value ReadOnly
to initiate a read-only connection to an Availability Group listener.URL: with sqlserver
scheme. username and password appears before the host. Any instance appears as
the first segment in the path. All other options are query parameters. Examples:
sqlserver://username:password@host/instance?param1=value¶m2=value
sqlserver://username:password@host:port?param1=value¶m2=value
sqlserver://sa@localhost/SQLExpress?database=master&connection+timeout=30
// `SQLExpress instance.sqlserver://sa:mypass@localhost?database=master&connection+timeout=30
// username=sa, password=mypass.sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30
// port 1234 on localhost.sqlserver://sa:my%7Bpass@somehost?connection+timeout=30
// password is "my{pass"A string of this format can be constructed using the URL
type in the net/url
package.
query := url.Values{}
query.Add("app name", "MyAppName")
u := &url.URL{
Scheme: "sqlserver",
User: url.UserPassword(username, password),
Host: fmt.Sprintf("%s:%d", hostname, port),
// Path: instance, // if connecting to an instance instead of a port
RawQuery: query.Encode(),
}
db, err := sql.Open("sqlserver", u.String())
ADO: key=value
pairs separated by ;
. Values may not contain ;
, leading and trailing whitespace is ignored.
Examples:
server=localhost\\SQLExpress;user id=sa;database=master;app name=MyAppName
server=localhost;user id=sa;database=master;app name=MyAppName
ODBC: Prefix with odbc
, key=value
pairs separated by ;
. Allow ;
by wrapping
values in {}
. Examples:
odbc:server=localhost\\SQLExpress;user id=sa;database=master;app name=MyAppName
odbc:server=localhost;user id=sa;database=master;app name=MyAppName
odbc:server=localhost;user id=sa;password={foo;bar}
// Value marked with {}
, password is "foo;bar"odbc:server=localhost;user id=sa;password={foo{bar}
// Value marked with {}
, password is "foo{bar"odbc:server=localhost;user id=sa;password={foobar }
// Value marked with {}
, password is "foobar "odbc:server=localhost;user id=sa;password=foo{bar
// Literal {
, password is "foo{bar"odbc:server=localhost;user id=sa;password=foo}bar
// Literal }
, password is "foo}bar"odbc:server=localhost;user id=sa;password={foo{bar}
// Literal {
, password is "foo{bar"odbc:server=localhost;user id=sa;password={foo}}bar}
// Escaped } with
}}`, password is "foo}bar"To run a stored procedure, set the query text to the procedure name:
var account = "abc"
_, err := db.ExecContext(ctx, "sp_RunMe",
sql.Named("ID", 123),
sql.Named("Account", sql.Out{Dest: &account}),
)
The sqlserver
driver uses normal MS SQL Server syntax and expects parameters in
the sql query to be in the form of either @Name
or @p1
to @pN
(ordinal position).
db.QueryContext(ctx, `select * from t where ID = @ID and Name = @p2;`, sql.Named("ID", 6), "Bob")
To pass specific types to the query parameters, say varchar
or date
types,
you must convert the types to the type before passing in. The following types
are supported:
select ID = convert(bigint, SCOPE_IDENTITY());
to the end of your
query (ref SCOPE_IDENTITY).
This will ensure you are getting the correct ID and will prevent a network round trip.go test
is used for testing. A running instance of MSSQL server is required.
Environment variables are used to pass login information.
Example:
env SQLSERVER_DSN=sqlserver://user:pass@hostname/instance?database=test1 go test
These features still exist in the driver, but they are are deprecated.
If you use the driver name "mssql" (rather then "sqlserver") the SQL text will be loosly parsed and an attempt to extract identifiers using one of
will be used. This is not recommended with SQL Server.
There is at least one existing won't fix
issue with the query parsing.
Use the native "@Name" parameters instead with the "sqlserver" driver name.