|
|
8 年之前 | |
|---|---|---|
| .. | ||
| LICENSE.txt | 8 年之前 | |
| README.md | 8 年之前 | |
| buf.go | 8 年之前 | |
| charset.go | 8 年之前 | |
| collation.go | 8 年之前 | |
| cp1250.go | 8 年之前 | |
| cp1251.go | 8 年之前 | |
| cp1252.go | 8 年之前 | |
| cp1253.go | 8 年之前 | |
| cp1254.go | 8 年之前 | |
| cp1255.go | 8 年之前 | |
| cp1256.go | 8 年之前 | |
| cp1257.go | 8 年之前 | |
| cp1258.go | 8 年之前 | |
| cp437.go | 8 年之前 | |
| cp850.go | 8 年之前 | |
| cp874.go | 8 年之前 | |
| cp932.go | 8 年之前 | |
| cp936.go | 8 年之前 | |
| cp949.go | 8 年之前 | |
| cp950.go | 8 年之前 | |
| decimal.go | 8 年之前 | |
| doc.go | 8 年之前 | |
| error.go | 8 年之前 | |
| log.go | 8 年之前 | |
| mssql.go | 8 年之前 | |
| mssql_go1.3.go | 8 年之前 | |
| mssql_go1.3pre.go | 8 年之前 | |
| mssql_go18.go | 8 年之前 | |
| net.go | 8 年之前 | |
| ntlm.go | 8 年之前 | |
| parser.go | 8 年之前 | |
| rpc.go | 8 年之前 | |
| sspi_windows.go | 8 年之前 | |
| tds.go | 8 年之前 | |
| token.go | 8 年之前 | |
| token_string.go | 8 年之前 | |
| tran.go | 8 年之前 | |
| types.go | 8 年之前 | |
| uniqueidentifier.go | 8 年之前 | |
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=30server=localhost;user id=sa;database=master;connection timeout=30ODBC: 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=30odbc:server=localhost;user id=sa;database=master;connection timeout=30odbc: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=valuesqlserver://username:password@host:port?param1=value¶m2=valuesqlserver://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