-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for AAD Service Principal connection
- Loading branch information
1 parent
1598eaf
commit dd2ce3e
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
_ "github.com/denisenkom/go-mssqldb" | ||
"github.com/denisenkom/go-mssqldb/azuread" | ||
) | ||
|
||
var ( | ||
debug = flag.Bool("debug", true, "enable debugging") | ||
password = flag.String("password", "", "the client secret for the app/client ID") | ||
port *int = flag.Int("port", 1433, "the database port") | ||
server = flag.String("server", "", "the database server") | ||
user = flag.String("user", "", "the app/client ID of the service principal") | ||
database = flag.String("database", "", "the database name") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *debug { | ||
fmt.Printf(" password:%s\n", *password) | ||
fmt.Printf(" port:%d\n", *port) | ||
fmt.Printf(" server:%s\n", *server) | ||
fmt.Printf(" user:%s\n", *user) | ||
fmt.Printf(" database:%s\n", *database) | ||
} | ||
|
||
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;fedauth=ActiveDirectoryServicePrincipal;", *server, *user, *password, *port, *database) | ||
if *debug { | ||
fmt.Printf(" connString:%s\n", connString) | ||
} | ||
conn, err := sql.Open(azuread.DriverName, connString) | ||
if err != nil { | ||
log.Fatal("Open connection failed:", err.Error()) | ||
} | ||
defer conn.Close() | ||
|
||
stmt, err := conn.Prepare("select 1, 'abc'") | ||
if err != nil { | ||
log.Fatal("Prepare failed:", err.Error()) | ||
} | ||
defer stmt.Close() | ||
|
||
row := stmt.QueryRow() | ||
var somenumber int64 | ||
var somechars string | ||
err = row.Scan(&somenumber, &somechars) | ||
if err != nil { | ||
log.Fatal("Scan failed:", err.Error()) | ||
} | ||
fmt.Printf("somenumber:%d\n", somenumber) | ||
fmt.Printf("somechars:%s\n", somechars) | ||
|
||
fmt.Printf("bye\n") | ||
} |