mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
135 lines
5.9 KiB
C#
135 lines
5.9 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Data.SqlClient;
|
|
|
|
namespace AzureSqlSample
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(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"; // update me
|
|
builder.Password = "your_password"; // update me
|
|
builder.InitialCatalog = "your_database_name"; // update me
|
|
|
|
// Connect to Azure SQL
|
|
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.");
|
|
|
|
using (SqlCommand command = new SqlCommand("DROP TABLE IF EXISTS Employees", connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
|
|
|
|
// 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.");
|
|
using (SqlCommand command = new SqlCommand("DROP TABLE IF EXISTS Employees", connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("All done. Press any key to finish...");
|
|
Console.ReadKey(true);
|
|
}
|
|
}
|
|
} |