Unknwon f967e9d021 vendor: add new dependency (#3772) | %!s(int64=7) %!d(string=hai) anos | |
---|---|---|
.. | ||
LICENSE.txt | %!s(int64=7) %!d(string=hai) anos | |
README.md | %!s(int64=7) %!d(string=hai) anos | |
buf.go | %!s(int64=7) %!d(string=hai) anos | |
charset.go | %!s(int64=7) %!d(string=hai) anos | |
collation.go | %!s(int64=7) %!d(string=hai) anos | |
cp1250.go | %!s(int64=7) %!d(string=hai) anos | |
cp1251.go | %!s(int64=7) %!d(string=hai) anos | |
cp1252.go | %!s(int64=7) %!d(string=hai) anos | |
cp1253.go | %!s(int64=7) %!d(string=hai) anos | |
cp1254.go | %!s(int64=7) %!d(string=hai) anos | |
cp1255.go | %!s(int64=7) %!d(string=hai) anos | |
cp1256.go | %!s(int64=7) %!d(string=hai) anos | |
cp1257.go | %!s(int64=7) %!d(string=hai) anos | |
cp1258.go | %!s(int64=7) %!d(string=hai) anos | |
cp437.go | %!s(int64=7) %!d(string=hai) anos | |
cp850.go | %!s(int64=7) %!d(string=hai) anos | |
cp874.go | %!s(int64=7) %!d(string=hai) anos | |
cp932.go | %!s(int64=7) %!d(string=hai) anos | |
cp936.go | %!s(int64=7) %!d(string=hai) anos | |
cp949.go | %!s(int64=7) %!d(string=hai) anos | |
cp950.go | %!s(int64=7) %!d(string=hai) anos | |
decimal.go | %!s(int64=7) %!d(string=hai) anos | |
doc.go | %!s(int64=7) %!d(string=hai) anos | |
error.go | %!s(int64=7) %!d(string=hai) anos | |
log.go | %!s(int64=7) %!d(string=hai) anos | |
mssql.go | %!s(int64=7) %!d(string=hai) anos | |
mssql_go1.3.go | %!s(int64=7) %!d(string=hai) anos | |
mssql_go1.3pre.go | %!s(int64=7) %!d(string=hai) anos | |
mssql_go18.go | %!s(int64=7) %!d(string=hai) anos | |
net.go | %!s(int64=7) %!d(string=hai) anos | |
ntlm.go | %!s(int64=7) %!d(string=hai) anos | |
parser.go | %!s(int64=7) %!d(string=hai) anos | |
rpc.go | %!s(int64=7) %!d(string=hai) anos | |
sspi_windows.go | %!s(int64=7) %!d(string=hai) anos | |
tds.go | %!s(int64=7) %!d(string=hai) anos | |
token.go | %!s(int64=7) %!d(string=hai) anos | |
token_string.go | %!s(int64=7) %!d(string=hai) anos | |
tran.go | %!s(int64=7) %!d(string=hai) anos | |
types.go | %!s(int64=7) %!d(string=hai) anos | |
uniqueidentifier.go | %!s(int64=7) %!d(string=hai) anos |
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