Skip to content

Commit 7726643

Browse files
authored
Merge pull request #604 from johnnypham/release-docs-2.0
Code samples for release 2.0 docs
2 parents fc6aece + dd727bf commit 7726643

6 files changed

+247
-8
lines changed

doc/samples/SqlBulkCopyOptions_Default.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ static void Main()
7272
}
7373
catch (Exception ex)
7474
{
75+
// Print the number of rows processed using the
76+
// RowsCopied property.
77+
Console.WriteLine("{0} rows were processed.",
78+
bulkCopy.RowsCopied);
7579
Console.WriteLine(ex.Message);
7680
}
7781
finally
@@ -82,6 +86,8 @@ static void Main()
8286

8387
// Perform a final count on the destination
8488
// table to see how many rows were added.
89+
// Note that for this scenario, the value will
90+
// not be equal to the RowsCopied property.
8591
long countEnd = System.Convert.ToInt32(
8692
commandRowCount.ExecuteScalar());
8793
Console.WriteLine("Ending row count = {0}", countEnd);
@@ -92,8 +98,8 @@ static void Main()
9298
}
9399

94100
private static string GetConnectionString()
95-
// To avoid storing the sourceConnection string in your code,
96-
// you can retrieve it from a configuration file.
101+
// To avoid storing the sourceConnection string in your code,
102+
// you can retrieve it from a configuration file.
97103
{
98104
return "Data Source=(local); " +
99105
" Integrated Security=true;" +

doc/samples/SqlBulkCopyOptions_UseInternalTransaction.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ static void Main()
7878
}
7979
catch (Exception ex)
8080
{
81+
// Print the number of rows processed using the
82+
// RowsCopied property.
83+
Console.WriteLine("{0} rows were processed.",
84+
bulkCopy.RowsCopied);
8185
Console.WriteLine(ex.Message);
8286
}
8387
finally
@@ -88,6 +92,8 @@ static void Main()
8892

8993
// Perform a final count on the destination
9094
// table to see how many rows were added.
95+
// Note that for this scenario, the value will
96+
// not be equal to the RowsCopied property.
9197
long countEnd = System.Convert.ToInt32(
9298
commandRowCount.ExecuteScalar());
9399
Console.WriteLine("Ending row count = {0}", countEnd);
@@ -98,8 +104,8 @@ static void Main()
98104
}
99105

100106
private static string GetConnectionString()
101-
// To avoid storing the sourceConnection string in your code,
102-
// you can retrieve it from a configuration file.
107+
// To avoid storing the sourceConnection string in your code,
108+
// you can retrieve it from a configuration file.
103109
{
104110
return "Data Source=(local); " +
105111
" Integrated Security=true;" +

doc/samples/SqlBulkCopy_ExternalTransaction.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ static void Main()
8181
}
8282
catch (Exception ex)
8383
{
84+
// Print the number of rows processed using the
85+
// RowsCopied property.
86+
Console.WriteLine("{0} rows were processed.",
87+
bulkCopy.RowsCopied);
8488
Console.WriteLine(ex.Message);
8589
transaction.Rollback();
8690
}
@@ -104,8 +108,8 @@ static void Main()
104108
}
105109

106110
private static string GetConnectionString()
107-
// To avoid storing the sourceConnection string in your code,
108-
// you can retrieve it from a configuration file.
111+
// To avoid storing the sourceConnection string in your code,
112+
// you can retrieve it from a configuration file.
109113
{
110114
return "Data Source=(local); " +
111115
" Integrated Security=true;" +

doc/samples/SqlBulkCopy_WriteToServer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ static void Main()
5454
{
5555
// Write from the source to the destination.
5656
bulkCopy.WriteToServer(reader);
57+
// Print the number of rows processed using the
58+
// RowsCopied property.
59+
Console.WriteLine("{0} rows were processed.",
60+
bulkCopy.RowsCopied);
5761
}
5862
catch (Exception ex)
5963
{
@@ -81,8 +85,8 @@ static void Main()
8185
}
8286

8387
private static string GetConnectionString()
84-
// To avoid storing the sourceConnection string in your code,
85-
// you can retrieve it from a configuration file.
88+
// To avoid storing the sourceConnection string in your code,
89+
// you can retrieve it from a configuration file.
8690
{
8791
return "Data Source=(local); " +
8892
" Integrated Security=true;" +
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Diagnostics.Tracing;
4+
using Microsoft.Data.SqlClient;
5+
6+
// This listener class will listen for events from the SqlClientEventSource class.
7+
// SqlClientEventSource is an implementation of the EventSource class which gives
8+
// it the ability to create events.
9+
public class SqlClientListener : EventListener
10+
{
11+
protected override void OnEventSourceCreated(EventSource eventSource)
12+
{
13+
// Only enable events from SqlClientEventSource.
14+
if (eventSource.Name.Equals("Microsoft.Data.SqlClient.EventSource"))
15+
{
16+
// Use EventKeyWord 2 to capture basic application flow events.
17+
// See the above table for all available keywords.
18+
EnableEvents(eventSource, EventLevel.Informational, (EventKeywords)2);
19+
}
20+
}
21+
22+
// This callback runs whenever an event is written by SqlClientEventSource.
23+
// Event data is accessed through the EventWrittenEventArgs parameter.
24+
protected override void OnEventWritten(EventWrittenEventArgs eventData)
25+
{
26+
// Print event data.
27+
Console.WriteLine(eventData.Payload[0]);
28+
}
29+
}
30+
31+
class Program
32+
{
33+
public static void Main()
34+
{
35+
// Create a new event listener.
36+
using (SqlClientListener listener = new SqlClientListener())
37+
{
38+
string connectionString = "Data Source=localhost; " +
39+
"Initial Catalog=AdventureWorks; Integrated Security=true";
40+
41+
// Open a connection to the AdventureWorks database.
42+
using (SqlConnection connection = new SqlConnection(connectionString))
43+
{
44+
connection.Open();
45+
46+
string sql = "SELECT * FROM Sales.Currency";
47+
SqlCommand command = new SqlCommand(sql, connection);
48+
49+
// Perform a data operation on the server.
50+
SqlDataReader reader = command.ExecuteReader();
51+
while (reader.Read())
52+
{
53+
// Read the data.
54+
}
55+
reader.Close();
56+
}
57+
}
58+
}
59+
}
60+
// </Snippet1>
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// <Snippet1>
2+
using System;
3+
using Microsoft.Data.SqlClient;
4+
using Microsoft.Data.SqlClient.DataClassification;
5+
6+
class Program
7+
{
8+
// Name of the temporary table created for this sample program.
9+
static string tableName = "SQLCLIENT_DATA_DISCOVERY_CLASSIFICATION";
10+
11+
public static void Main()
12+
{
13+
// To avoid storing the connection string in your code, you can retrieve it from a configuration file.
14+
string connectionString = "Data Source=localhost; Integrated Security=true; Initial Catalog=AdventureWorks;";
15+
16+
// Open a connection to the AdventureWorks database.
17+
using (SqlConnection connection = new SqlConnection(connectionString))
18+
{
19+
connection.Open();
20+
21+
try
22+
{
23+
// Check if the target SQL Server supports Data Discovery and Classification.
24+
if (DataClassificationSupported(connection))
25+
{
26+
// Create the temporary table and retrieve its Data Discovery and Classification information.
27+
CreateTable(connection);
28+
RunTests(connection);
29+
}
30+
}
31+
finally
32+
{
33+
// Drop the temporary table.
34+
DropTable(connection);
35+
}
36+
}
37+
}
38+
39+
/// <summary>
40+
/// Verifies if SQL Data Discovery and Classification feature is available on the target server.
41+
/// </summary>
42+
/// <param name="connection">The SqlConnection to work with.</param>
43+
/// <returns>True if the target SQL Server supports the feature and false otherwise.</returns>
44+
public static bool DataClassificationSupported(SqlConnection connection)
45+
{
46+
try
47+
{
48+
SqlCommand command = new SqlCommand(null, connection);
49+
command.CommandText = "SELECT * FROM SYS.SENSITIVITY_CLASSIFICATIONS";
50+
command.ExecuteNonQuery();
51+
}
52+
catch (SqlException e)
53+
{
54+
// Error 208: Object Not Found
55+
if (e.Errors != null && e.Errors[0].Number == 208)
56+
{
57+
Console.WriteLine("This feature is not supported on the target SQL Server.");
58+
return false;
59+
}
60+
}
61+
return true;
62+
}
63+
64+
/// <summary>
65+
/// Creates a temporary table for this sample program and sets tags for Sensitivity Classification.
66+
/// </summary>
67+
/// <param name="connection">The SqlConnection to work with.</param>
68+
private static void CreateTable(SqlConnection connection)
69+
{
70+
SqlCommand command = new SqlCommand(null, connection);
71+
72+
// Creates table for storing Supplier data.
73+
command.CommandText = $"CREATE TABLE {tableName} ("
74+
+ "[Id] [int] IDENTITY(1,1) NOT NULL,"
75+
+ "[CompanyName] [nvarchar](40) NOT NULL,"
76+
+ "[ContactName] [nvarchar](50) NULL,"
77+
+ "[ContactTitle] [nvarchar](40) NULL,"
78+
+ "[City] [nvarchar](40) NULL,"
79+
+ "[CountryName] [nvarchar](40) NULL,"
80+
+ "[Phone] [nvarchar](30) MASKED WITH (FUNCTION = 'default()') NULL,"
81+
+ "[Fax] [nvarchar](30) MASKED WITH (FUNCTION = 'default()') NULL)";
82+
command.ExecuteNonQuery();
83+
84+
// Set Sensitivity Classification tags for table columns.
85+
command.CommandText = $"ADD SENSITIVITY CLASSIFICATION TO {tableName}"
86+
+ ".CompanyName WITH (LABEL='PII', LABEL_ID='L1', INFORMATION_TYPE='Company Name', INFORMATION_TYPE_ID='COMPANY')";
87+
command.ExecuteNonQuery();
88+
89+
command.CommandText = $"ADD SENSITIVITY CLASSIFICATION TO {tableName}"
90+
+ ".ContactName WITH (LABEL='PII', LABEL_ID='L1', INFORMATION_TYPE='Person Name', INFORMATION_TYPE_ID='NAME')";
91+
command.ExecuteNonQuery();
92+
93+
command.CommandText = $"ADD SENSITIVITY CLASSIFICATION TO {tableName}"
94+
+ ".Phone WITH (LABEL='PII', LABEL_ID='L1', INFORMATION_TYPE='Contact Information', INFORMATION_TYPE_ID='CONTACT')";
95+
command.ExecuteNonQuery();
96+
97+
command.CommandText = $"ADD SENSITIVITY CLASSIFICATION TO {tableName}"
98+
+ ".Fax WITH (LABEL='PII', LABEL_ID='L1', INFORMATION_TYPE='Contact Information', INFORMATION_TYPE_ID='CONTACT')";
99+
command.ExecuteNonQuery();
100+
}
101+
102+
/// <summary>
103+
/// Run query to fetch result set from target table.
104+
/// </summary>
105+
/// <param name="connection">The SqlConnection to work with.</param>
106+
private static void RunTests(SqlConnection connection)
107+
{
108+
SqlCommand command = new SqlCommand(null, connection);
109+
command.CommandText = $"SELECT * FROM {tableName}";
110+
using (SqlDataReader reader = command.ExecuteReader())
111+
{
112+
PrintSensitivityClassification(reader);
113+
}
114+
}
115+
116+
/// <summary>
117+
/// Prints Sensitivity Classification data as received in the result set.
118+
/// </summary>
119+
/// <param name="reader">The SqlDataReader to work with.</param>
120+
private static void PrintSensitivityClassification(SqlDataReader reader)
121+
{
122+
if (reader.SensitivityClassification != null)
123+
{
124+
for (int columnPos = 0; columnPos < reader.SensitivityClassification.ColumnSensitivities.Count; columnPos++)
125+
{
126+
foreach (SensitivityProperty sp in reader.SensitivityClassification.ColumnSensitivities[columnPos].SensitivityProperties)
127+
{
128+
if (sp.Label != null)
129+
{
130+
Console.WriteLine($"Labels received for Column : {columnPos}");
131+
Console.WriteLine($"Label ID: {sp.Label.Id}");
132+
Console.WriteLine($"Label Name: {sp.Label.Name}");
133+
Console.WriteLine();
134+
}
135+
136+
if (sp.InformationType != null)
137+
{
138+
Console.WriteLine($"Information Types received for Column : {columnPos}");
139+
Console.WriteLine($"Information Type ID: {sp.InformationType.Id}");
140+
Console.WriteLine($"Information Type: {sp.InformationType.Name}");
141+
Console.WriteLine();
142+
}
143+
}
144+
}
145+
}
146+
}
147+
148+
/// <summary>
149+
/// Deletes the table created for this sample program.
150+
/// </summary>
151+
/// <param name="connection">The SqlConnection to work with.</param>
152+
private static void DropTable(SqlConnection connection)
153+
{
154+
SqlCommand command = new SqlCommand(null, connection);
155+
command.CommandText = $"DROP TABLE {tableName}";
156+
command.ExecuteNonQuery();
157+
}
158+
}
159+
// </Snippet1>

0 commit comments

Comments
 (0)