訓練
認證
Microsoft Certified: Azure Database Administrator Associate - Certifications
使用 Microsoft PaaS 關聯式資料庫供應項目管理用於雲端、內部部署和混合關聯式資料庫的 SQL Server 資料庫基礎結構。
適用於: Azure SQL 資料庫
Azure SQL 受控執行個體
在此快速入門中,您將使用 Golang 程式設計語言,透過 go-mssqldb 驅動程式連線至 Azure SQL 資料庫或 Azure SQL 受控執行個體資料庫。 範例使用明確的 Transact-SQL (T-SQL) 陳述式來查詢和修改資料。 Golang 是開放原始碼程式設計語言,可讓您輕鬆地建置簡單、可靠、高效的軟體。
若要完成本快速入門,您需要:
具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶。
Azure SQL 資料庫或 Azure SQL 受控執行個體資料庫。 您可以使用其中一個快速入門來建立資料庫:
SQL Database | SQL 受控執行個體 | Azure VM 上的 SQL Server |
---|---|---|
建立 | ||
入口網站 | 入口網站 | 入口網站 |
CLI | CLI | |
PowerShell | PowerShell | PowerShell |
設定 | ||
伺服器層級 IP 防火牆規則 | VM 的連線能力 | |
來自內部部署的連線 | 連線到 SQL Server 執行個體 | |
載入資料 | ||
每個快速入門載入的 AdventureWorks2022 |
還原 WideWorldImporters | 還原 WideWorldImporters |
從 GitHub 的 BACPAC 檔案還原或匯入 AdventureWorks2022 |
從 GitHub 的 BACPAC 檔案還原或匯入 AdventureWorks2022 |
重要
本文中的指令碼撰寫為使用 AdventureWorks2022
資料庫。 對於 SQL 受控執行個體,必須將 AdventureWorks2022
資料庫匯入執行個體資料庫中,或將本文中的指令碼修改為使用 Wide World Importers 資料庫。
適用於所安裝作業系統的 Go 和相關軟體。
適用於所安裝作業系統的最新版本的 sqlcmd。
適用於所安裝作業系統的 Azure PowerShell Az 模組。
取得連線到資料庫所需的連線資訊。 在後續程序中,您將需要完整的伺服器名稱或主機名稱、資料庫名稱和登入資訊。
登入 Azure 入口網站。
瀏覽至 [SQL Database] 或 [SQL 受控執行個體] 頁面。
在 [概觀] 頁面上,針對 Azure SQL 資料庫中的資料庫檢閱 [伺服器名稱] 旁的完整伺服器名稱,若為 Azure SQL 受控執行個體或 Azure VM 上的 SQL Server,則檢閱 [主機] 旁的完整伺服器名稱 (或 IP 位址)。 若要複製伺服器名稱或主機名稱,請將滑鼠暫留在其上方,然後選取 [複製] 圖示。
注意
如需 Azure VM 上的 SQL Server 連線資訊,請參閱連線到 SQL Server 執行個體。
從終端機建立名為 SqlServerSample
的新專案資料夾。
mkdir SqlServerSample
透過文字編輯器在 SqlServerSample
資料夾中建立名為 CreateTestData.sql
的檔案。 在檔案中,貼上此 T-SQL 程式碼,以建立結構描述、資料表,並插入幾個資料列。
CREATE SCHEMA TestSchema;
GO
CREATE TABLE TestSchema.Employees (
Id INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
Name NVARCHAR(50),
Location NVARCHAR(50)
);
GO
INSERT INTO TestSchema.Employees (Name, Location)
VALUES (N'Jared', N'Australia'),
(N'Nikita', N'India'),
(N'Astrid', N'Germany');
GO
SELECT * FROM TestSchema.Employees;
GO
在命令提示字元中,瀏覽至 SqlServerSample
,並使用 sqlcmd
來連線到資料庫,並執行新建立的 Azure SQL 指令碼。 替換伺服器和資料庫的適當值。
az login
sqlcmd -S <your_server>.database.windows.net -G -d <your_database> -i ./CreateTestData.sql
在 sample.go
資料夾中,建立名為 SqlServerSample
的檔案。
在檔案中貼上此程式碼。 新增伺服器和資料庫的值。 此範例使用 Golang 內容方法來確定有作用中的連線。
package main
import (
"github.com/microsoft/go-mssqldb/azuread"
"database/sql"
"context"
"log"
"fmt"
"errors"
)
var db *sql.DB
var server = "<your_server.database.windows.net>"
var port = 1433
var database = "<your_database>"
func main() {
// Build connection string
connString := fmt.Sprintf("server=%s;port=%d;database=%s;fedauth=ActiveDirectoryDefault;", server, port, database)
var err error
// Create connection pool
db, err = sql.Open(azuread.DriverName, connString)
if err != nil {
log.Fatal("Error creating connection pool: ", err.Error())
}
ctx := context.Background()
err = db.PingContext(ctx)
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("Connected!\n")
// Create employee
createID, err := CreateEmployee("Jake", "United States")
if err != nil {
log.Fatal("Error creating Employee: ", err.Error())
}
fmt.Printf("Inserted ID: %d successfully.\n", createID)
// Read employees
count, err := ReadEmployees()
if err != nil {
log.Fatal("Error reading Employees: ", err.Error())
}
fmt.Printf("Read %d row(s) successfully.\n", count)
// Update from database
updatedRows, err := UpdateEmployee("Jake", "Poland")
if err != nil {
log.Fatal("Error updating Employee: ", err.Error())
}
fmt.Printf("Updated %d row(s) successfully.\n", updatedRows)
// Delete from database
deletedRows, err := DeleteEmployee("Jake")
if err != nil {
log.Fatal("Error deleting Employee: ", err.Error())
}
fmt.Printf("Deleted %d row(s) successfully.\n", deletedRows)
}
// CreateEmployee inserts an employee record
func CreateEmployee(name string, location string) (int64, error) {
ctx := context.Background()
var err error
if db == nil {
err = errors.New("CreateEmployee: db is null")
return -1, err
}
// Check if database is alive.
err = db.PingContext(ctx)
if err != nil {
return -1, err
}
tsql := `
INSERT INTO TestSchema.Employees (Name, Location) VALUES (@Name, @Location);
select isNull(SCOPE_IDENTITY(), -1);
`
stmt, err := db.Prepare(tsql)
if err != nil {
return -1, err
}
defer stmt.Close()
row := stmt.QueryRowContext(
ctx,
sql.Named("Name", name),
sql.Named("Location", location))
var newID int64
err = row.Scan(&newID)
if err != nil {
return -1, err
}
return newID, nil
}
// ReadEmployees reads all employee records
func ReadEmployees() (int, error) {
ctx := context.Background()
// Check if database is alive.
err := db.PingContext(ctx)
if err != nil {
return -1, err
}
tsql := fmt.Sprintf("SELECT Id, Name, Location FROM TestSchema.Employees;")
// Execute query
rows, err := db.QueryContext(ctx, tsql)
if err != nil {
return -1, err
}
defer rows.Close()
var count int
// Iterate through the result set.
for rows.Next() {
var name, location string
var id int
// Get values from row.
err := rows.Scan(&id, &name, &location)
if err != nil {
return -1, err
}
fmt.Printf("ID: %d, Name: %s, Location: %s\n", id, name, location)
count++
}
return count, nil
}
// UpdateEmployee updates an employee's information
func UpdateEmployee(name string, location string) (int64, error) {
ctx := context.Background()
// Check if database is alive.
err := db.PingContext(ctx)
if err != nil {
return -1, err
}
tsql := fmt.Sprintf("UPDATE TestSchema.Employees SET Location = @Location WHERE Name = @Name")
// Execute non-query with named parameters
result, err := db.ExecContext(
ctx,
tsql,
sql.Named("Location", location),
sql.Named("Name", name))
if err != nil {
return -1, err
}
return result.RowsAffected()
}
// DeleteEmployee deletes an employee from the database
func DeleteEmployee(name string) (int64, error) {
ctx := context.Background()
// Check if database is alive.
err := db.PingContext(ctx)
if err != nil {
return -1, err
}
tsql := fmt.Sprintf("DELETE FROM TestSchema.Employees WHERE Name = @Name;")
// Execute non-query with named parameters
result, err := db.ExecContext(ctx, tsql, sql.Named("Name", name))
if err != nil {
return -1, err
}
return result.RowsAffected()
}
在命令提示字元中,執行下列命令以瀏覽至 SqlServerSample
並安裝適用於 Go 的 SQL Server 驅動程式。
go mod init SqlServerSample
go mod tidy
在命令提示字元中,執行下列命令。
az login
go run sample.go
驗證輸出。
Connected!
Inserted ID: 4 successfully.
ID: 1, Name: Jared, Location: Australia
ID: 2, Name: Nikita, Location: India
ID: 3, Name: Astrid, Location: Germany
ID: 4, Name: Jake, Location: United States
Read 4 row(s) successfully.
Updated 1 row(s) successfully.
Deleted 1 row(s) successfully.
訓練
認證
Microsoft Certified: Azure Database Administrator Associate - Certifications
使用 Microsoft PaaS 關聯式資料庫供應項目管理用於雲端、內部部署和混合關聯式資料庫的 SQL Server 資料庫基礎結構。
文件
適用於 Microsoft SQL Server 的 Microsoft Golang 驅動程式 - SQL Server
了解如何使用適用於 SQL Server 和 Azure SQL 資料庫 的 Golang 驅動程式,從以 Go 語言撰寫的任何應用程式啟用連線。