forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathexample_connection_test.go
More file actions
111 lines (90 loc) · 4.22 KB
/
example_connection_test.go
File metadata and controls
111 lines (90 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package mssql_test
import (
"fmt"
"net/url"
_ "github.com/microsoft/go-mssqldb"
)
// These examples demonstrate common usage patterns for the go-mssqldb driver.
// Note: sql.Open does not establish a network connection until the first use
// of the returned *sql.DB. These examples show code patterns without requiring
// a live SQL Server instance.
// Example_connectionString shows the basic connection string format.
// Use "sqlserver" as the driver name (not "mssql").
func Example_connectionString() {
// URL format connection string (recommended)
connStr := "sqlserver://user:password@localhost:1433?database=mydb"
fmt.Println("URL format:", connStr)
// ADO format connection string
adoConnStr := "server=localhost;user id=sa;password=secret;database=mydb"
fmt.Println("ADO format:", adoConnStr)
// ODBC format connection string
odbcConnStr := "odbc:server=localhost;user id=sa;password=secret;database=mydb"
fmt.Println("ODBC format:", odbcConnStr)
// Output:
// URL format: sqlserver://user:password@localhost:1433?database=mydb
// ADO format: server=localhost;user id=sa;password=secret;database=mydb
// ODBC format: odbc:server=localhost;user id=sa;password=secret;database=mydb
}
// Example_namedParameterSyntax shows the correct parameter syntax for queries.
// Use @ParameterName with sql.Named() for named parameters.
func Example_namedParameterSyntax() {
// Named parameter syntax - use @ParameterName
query := "SELECT * FROM users WHERE id = @ID AND active = @Active"
fmt.Println("Named parameters:", query)
// Positional parameter syntax - use @p1, @p2, etc.
positionalQuery := "SELECT * FROM users WHERE id = @p1 AND active = @p2"
fmt.Println("Positional parameters:", positionalQuery)
// Output:
// Named parameters: SELECT * FROM users WHERE id = @ID AND active = @Active
// Positional parameters: SELECT * FROM users WHERE id = @p1 AND active = @p2
}
// Example_buildConnectionString demonstrates programmatically building a connection string
// using the net/url package.
func Example_buildConnectionString() {
query := url.Values{}
query.Add("database", "mydb")
query.Add("connection timeout", "30")
query.Add("encrypt", "true")
u := &url.URL{
Scheme: "sqlserver",
User: url.UserPassword("username", "password"),
Host: "localhost:1433",
RawQuery: query.Encode(),
}
connStr := u.String()
fmt.Println("Built connection string")
// Use with sql.Open:
// db, err := sql.Open("sqlserver", connStr)
_ = connStr
// Output:
// Built connection string
}
// Example_azureADConnection shows connection strings for Azure AD authentication.
// Import "github.com/microsoft/go-mssqldb/azuread" and use azuread.DriverName.
// Always enable encryption with certificate validation for Azure SQL.
func Example_azureADConnection() {
// DefaultAzureCredential (recommended for most scenarios)
// Enable TLS with certificate validation for Azure SQL
defaultCred := "sqlserver://server.database.windows.net?database=mydb&fedauth=ActiveDirectoryDefault&encrypt=true&TrustServerCertificate=false"
fmt.Println("DefaultAzureCredential:", defaultCred)
// Managed Identity
msiCred := "sqlserver://server.database.windows.net?database=mydb&fedauth=ActiveDirectoryMSI&encrypt=true&TrustServerCertificate=false"
fmt.Println("Managed Identity:", msiCred)
// Output:
// DefaultAzureCredential: sqlserver://server.database.windows.net?database=mydb&fedauth=ActiveDirectoryDefault&encrypt=true&TrustServerCertificate=false
// Managed Identity: sqlserver://server.database.windows.net?database=mydb&fedauth=ActiveDirectoryMSI&encrypt=true&TrustServerCertificate=false
}
// Example_storedProcedureSyntax shows how to call stored procedures with output parameters.
func Example_storedProcedureSyntax() {
// Stored procedure call syntax with named parameters
procCall := "EXEC sp_MyProcedure @InputParam = @Input, @OutputParam = @Output OUTPUT"
fmt.Println("Stored procedure:", procCall)
// In Go code, use sql.Named with sql.Out for output parameters:
// var outputValue string
// _, err := db.ExecContext(ctx, "sp_MyProcedure",
// sql.Named("Input", "test value"),
// sql.Named("Output", sql.Out{Dest: &outputValue}),
// )
// Output:
// Stored procedure: EXEC sp_MyProcedure @InputParam = @Input, @OutputParam = @Output OUTPUT
}