Unknwon f967e9d021 vendor: add new dependency (#3772) | 7 éve | |
---|---|---|
.. | ||
LICENSE.txt | 7 éve | |
README.md | 7 éve | |
buf.go | 7 éve | |
charset.go | 7 éve | |
collation.go | 7 éve | |
cp1250.go | 7 éve | |
cp1251.go | 7 éve | |
cp1252.go | 7 éve | |
cp1253.go | 7 éve | |
cp1254.go | 7 éve | |
cp1255.go | 7 éve | |
cp1256.go | 7 éve | |
cp1257.go | 7 éve | |
cp1258.go | 7 éve | |
cp437.go | 7 éve | |
cp850.go | 7 éve | |
cp874.go | 7 éve | |
cp932.go | 7 éve | |
cp936.go | 7 éve | |
cp949.go | 7 éve | |
cp950.go | 7 éve | |
decimal.go | 7 éve | |
doc.go | 7 éve | |
error.go | 7 éve | |
log.go | 7 éve | |
mssql.go | 7 éve | |
mssql_go1.3.go | 7 éve | |
mssql_go1.3pre.go | 7 éve | |
mssql_go18.go | 7 éve | |
net.go | 7 éve | |
ntlm.go | 7 éve | |
parser.go | 7 éve | |
rpc.go | 7 éve | |
sspi_windows.go | 7 éve | |
tds.go | 7 éve | |
token.go | 7 éve | |
token_string.go | 7 éve | |
tran.go | 7 éve | |
types.go | 7 éve | |
uniqueidentifier.go | 7 éve |
go get github.com/denisenkom/go-mssqldb
The connection string can be specified in one of three formats:
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;connection timeout=30
server=localhost;user id=sa;database=master;connection timeout=30
ODBC: Prefix with odbc
, key=value
pairs separated by ;
. Allow ;
by wrapping
values in {}
. Examples:
odbc:server=localhost\\SQLExpress;user id=sa;database=master;connection timeout=30
odbc:server=localhost;user id=sa;database=master;connection timeout=30
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"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("connection timeout", fmt.Sprintf("%d", connectionTimeout))
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(),
}
connectionString := u.String()
db, err := sql.Open("sqlserver", connectionString)
// or
db, err := sql.Open("mssql", connectionString)
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;`, sql.Named("ID", 6))
For the mssql
driver, the SQL statement text will be processed and literals will
be replaced by a parameter that matches one of the following:
where nnn represents an integer that specifies a 1-indexed positional parameter. Ex:
db.Query("SELECT * FROM t WHERE a = ?3, b = ?2, c = ?1", "x", "y", "z")
will expand to roughly
SELECT * FROM t WHERE a = 'z', b = 'y', c = 'x'
go test
is used for testing. A running instance of MSSQL server is required.
Environment variables are used to pass login information.
Example:
env HOST=localhost SQLUSER=sa SQLPASSWORD=sa DATABASE=test go test