|
| 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