mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
160 lines
7.3 KiB
C#
160 lines
7.3 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Data.SqlClient;
|
|
using Microsoft.Azure.KeyVault;
|
|
using Microsoft.Azure.KeyVault.Models;
|
|
using Microsoft.Azure.Services.AppAuthentication;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AzureSQLSample
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
Task task = Program.DoWork(args);
|
|
// Becuase this program takes user input, have a long wait.
|
|
var result = task.Wait(TimeSpan.FromMinutes(30));
|
|
}
|
|
|
|
static async Task DoWork(string[] args)
|
|
{
|
|
string sql;
|
|
|
|
// Build connection string
|
|
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
|
|
builder.DataSource = "your_server_name.database.windows.net"; // update me
|
|
builder.UserID = "your_user_id"; // update me
|
|
builder.Password = await GetPasswordFromKeyVault(); // taken from Key Vault
|
|
builder.InitialCatalog = "your_database"; // Update me
|
|
|
|
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine("Connect to Azure SQL and demo Create, Read, Update and Delete operations.");
|
|
|
|
// Connect to Azure SQL
|
|
Console.Write("Connecting to Azure SQL ... ");
|
|
connection.Open();
|
|
Console.WriteLine("Done.");
|
|
|
|
string dropTableIfExists = @"DROP TABLE IF EXISTS Employees";
|
|
using (SqlCommand command = new SqlCommand(dropTableIfExists, connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
Console.WriteLine("Done.");
|
|
}
|
|
|
|
|
|
// Create a Table and insert some sample data
|
|
Console.Write("Creating sample table with data, press any key to continue...");
|
|
Console.ReadKey(true);
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append("CREATE TABLE Employees ( ");
|
|
sb.Append(" Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, ");
|
|
sb.Append(" Name NVARCHAR(50), ");
|
|
sb.Append(" Location NVARCHAR(50) ");
|
|
sb.Append("); ");
|
|
sb.Append("INSERT INTO Employees (Name, Location) VALUES ");
|
|
sb.Append("(N'Jared', N'Australia'), ");
|
|
sb.Append("(N'Nikita', N'India'), ");
|
|
sb.Append("(N'Tom', N'Germany'); ");
|
|
sql = sb.ToString();
|
|
using (SqlCommand command = new SqlCommand(sql, connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
Console.WriteLine("Done.");
|
|
}
|
|
|
|
// INSERT demo
|
|
Console.Write("Inserting a new row into table, press any key to continue...");
|
|
Console.ReadKey(true);
|
|
sb.Clear();
|
|
sb.Append("INSERT Employees (Name, Location) ");
|
|
sb.Append("VALUES (@name, @location);");
|
|
sql = sb.ToString();
|
|
using (SqlCommand command = new SqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", "Jake");
|
|
command.Parameters.AddWithValue("@location", "United States");
|
|
int rowsAffected = command.ExecuteNonQuery();
|
|
Console.WriteLine(rowsAffected + " row(s) inserted");
|
|
}
|
|
|
|
// UPDATE demo
|
|
String userToUpdate = "Nikita";
|
|
Console.Write("Updating 'Location' for user '" + userToUpdate + "', press any key to continue...");
|
|
Console.ReadKey(true);
|
|
sb.Clear();
|
|
sb.Append("UPDATE Employees SET Location = N'United States' WHERE Name = @name");
|
|
sql = sb.ToString();
|
|
using (SqlCommand command = new SqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", userToUpdate);
|
|
int rowsAffected = command.ExecuteNonQuery();
|
|
Console.WriteLine(rowsAffected + " row(s) updated");
|
|
}
|
|
|
|
// DELETE demo
|
|
String userToDelete = "Jared";
|
|
Console.Write("Deleting user '" + userToDelete + "', press any key to continue...");
|
|
Console.ReadKey(true);
|
|
sb.Clear();
|
|
sb.Append("DELETE FROM Employees WHERE Name = @name;");
|
|
sql = sb.ToString();
|
|
using (SqlCommand command = new SqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", userToDelete);
|
|
int rowsAffected = command.ExecuteNonQuery();
|
|
Console.WriteLine(rowsAffected + " row(s) deleted");
|
|
}
|
|
|
|
// READ demo
|
|
Console.WriteLine("Reading data from table, press any key to continue...");
|
|
Console.ReadKey(true);
|
|
sql = "SELECT Id, Name, Location FROM Employees;";
|
|
using (SqlCommand command = new SqlCommand(sql, connection))
|
|
{
|
|
|
|
using (SqlDataReader reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
Console.WriteLine("{0} {1} {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (SqlException e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
}
|
|
finally
|
|
{
|
|
Console.WriteLine("Cleaning up table.");
|
|
string dropTableIfExists = @"DROP TABLE IF EXISTS Employees";
|
|
using (SqlCommand command = new SqlCommand(dropTableIfExists, connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
Console.WriteLine("Done.");
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("All done. Press any key to finish...");
|
|
Console.ReadKey(true);
|
|
}
|
|
|
|
private static async Task<string> GetPasswordFromKeyVault()
|
|
{
|
|
Console.WriteLine("Trying to get Password from Key Vault. Press a key to continue...");
|
|
Console.ReadKey(true);
|
|
/* The next four lines of code show you how to use AppAuthentication library to fetch secrets from your key vault */
|
|
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
|
|
KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
|
|
SecretBundle secret = await keyVaultClient.GetSecretAsync("https://your_key_vault_name.vault.azure.net/secrets/AppSecret"); // update me
|
|
return secret.Value;
|
|
}
|
|
}
|
|
} |