Skip to content

Instantly share code, notes, and snippets.

@MindFlavor
Last active September 16, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MindFlavor/0a5302058a19ddc99105 to your computer and use it in GitHub Desktop.
Save MindFlavor/0a5302058a19ddc99105 to your computer and use it in GitHub Desktop.
A very simple Always encrypted demo program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
class Program
{
const string CONNECTION_STRING_PATTERN = "Data Source={0:S}; Integrated Security=true; Column Encryption Setting=enabled;Initial Catalog=alwaysEnc";
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("!! Syantax error.\nMust specify the data source as parameter (ie SQL Server instance)");
return;
}
string sConn = string.Format(CONNECTION_STRING_PATTERN, args[0]);
try {
Insert(sConn, DateTime.Parse("2015-01-01"), 100.0);
Insert(sConn, DateTime.Parse("2015-01-07"), 97.5);
Insert(sConn, DateTime.Parse("2015-01-14"), 98.1);
Insert(sConn, DateTime.Parse("2015-01-20"), 99.7);
Insert(sConn, DateTime.Parse("2015-01-27"), 106.3);
Insert(sConn, DateTime.Parse("2015-01-27"), 106.3);
Insert(sConn, DateTime.Parse("2015-01-27"), 106.3);
Insert(sConn, DateTime.Parse("2015-01-27"), 106.3);
} catch(Exception exce)
{
Console.WriteLine(exce.Message);
}
Console.WriteLine("\nPress ENTER to exit...");
Console.ReadLine();
}
static void Insert(string connectionString, DateTime eventTime, double dWeight)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO ae.fat(EventTime, [Weight_Det], [Weight_Rand]) VALUES(@dt, @weight_det, @weight_rand)", conn))
{
SqlParameter p = new SqlParameter("@dt", System.Data.SqlDbType.DateTime);
p.Value = eventTime;
cmd.Parameters.Add(p);
p = new SqlParameter("@weight_det", System.Data.SqlDbType.Float);
p.Value = dWeight;
cmd.Parameters.Add(p);
// must use two separate parameters since they cannot be
// both deterministic and non-deterministic at the same time
p = new SqlParameter("@weight_rand", System.Data.SqlDbType.Float);
p.Value = dWeight;
cmd.Parameters.Add(p);
int i = cmd.ExecuteNonQuery();
Console.WriteLine("{0:N0} items inserted", i);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment