Skip to content

Conversation

@ErikEJ
Copy link
Contributor

@ErikEJ ErikEJ commented Nov 29, 2025

Scope: Make samples build with nor or minimal changes to actual snippets used in docs.

Added namespaces to organize code and prevent naming conflicts. Used filename as namespace, and for files with names of classes, added "CS" to namespace

Adjusted using directives to include necessary references to make build pass.

Wrapped code snippets in conditional compilation directives for mostly .NET Framework-specific APIs.

Made some changes to the .csproj file to enable local "dotnet build" - happy to revert as needed.

I will comment on any other type of changes made to a few samples.

Unsure about the sample for Microsoft.SqlServer.Server, but can revert these if needed. (and not build them)

Related to issue #3725

Scope: Make samples build with nor or minimal changes to actual snippets usied in docs.

Added namespaces to organize code and prevent naming conflicts. Used filename as namespace, and for files with names of classes, added "CS" to namespace

Adjusted `using` directives to include necessary references to make build pass.

Wrapped code snippets in conditional compilation directives for mostly  .NET Framework-specific APIs.

Made some changes to the .csproj file to enable local "dotnet build" - happy to revert as needed.
Copilot AI review requested due to automatic review settings November 29, 2025 13:25
@ErikEJ ErikEJ requested a review from a team as a code owner November 29, 2025 13:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors sample code files to enable building with minimal changes to documentation snippets. The primary changes include adding namespace declarations to prevent naming conflicts and wrapping .NET Framework-specific code in conditional compilation directives.

  • Adds unique namespaces to all sample files, typically using the filename as the namespace name (with "CS" suffix for files with class names)
  • Wraps .NET Framework-specific code in #if NETFRAMEWORK directives
  • Adds missing using directives required for compilation
  • Updates project configuration to support multi-targeting (net8.0 and net462)

Reviewed changes

Copilot reviewed 228 out of 228 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
TransactionIsolationLevels.cs Changed namespace from generic to filename-based
SqlVectorExample.cs Added namespace, missing using directives, and changed async to sync method call
SqlUserDefinedType*.cs Added namespaces and changed namespace references from Microsoft.Data.SqlClient.Server to Microsoft.SqlServer.Server
SqlRowUpdatingEventArgs.cs, SqlParameterCollection_*.cs, SqlDataAdapter_SelectCommand.cs, etc. Wrapped .NET Framework-specific code in conditional compilation directives
SqlDataReader_DataDiscoveryAndClassification.cs Fixed typo in Console method name
SqlDataAdapter_Properties.cs Removed nested namespace and unnecessary using directive
SqlCommand_ExecuteNonQuery_SP_DML.cs Removed unused variable declaration
SqlDataAdapter_SPIdentityReturn.cs Changed Int to int for type consistency
SqlConfigurableRetryLogic_SqlCommand.cs Added wrapper classes to separate code snippets
SqlClientEventSource.cs, SqlClientDiagnosticCounter.cs Added namespaces and missing using directives
RegisterCustomKeyStoreProvider_*.cs Added namespaces, using directives, and stub implementations for custom providers
SqlUserDefinedTypeAttribute_Type1.cs Fixed typo in Convert.Toint method calls
Microsoft.Data.SqlClient.Samples.csproj Added multi-targeting configuration and package reference
ConnectionStrings_Encrypt.cs Renamed parameter for clarity

@@ -1,4 +1,5 @@
using System;
/*
Copy link
Contributor Author

@ErikEJ ErikEJ Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excluded this from build, as it contains obsolete APIs

@@ -1,4 +1,8 @@
using System;
/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could not make this build, so commented out

}
// <Snippet1>
static void ToggleConfigEncryption(string exeFile)
static void ToggleConfigEncryption(string exeConfigName)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected parameter name to make build pass

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net462</TargetFrameworks>
Copy link
Contributor Author

@ErikEJ ErikEJ Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to make "dotnet build" work

<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="System.Configuration.ConfigurationManager" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to make build pass


<!-- .NET Framework references -->
<ItemGroup Condition="$(TargetGroup) == 'netfx'">
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to make local "dotnet build" work

Point pt = new Point();
pt.X = Convert.Toint(xy[0]);
pt.Y = Convert.Toint(xy[1]);
pt.X = Convert.ToInt32(xy[0]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix of incorrect sample code

class Program
{
static void Main()
static void Main(string connectionString)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added missing parameter to make build pass

{
customKeyStoreProviders.Clear();
SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider();
SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(new DefaultAzureCredential());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uses the updated API to make build pass

}
// </Snippet1>

class MyCustomKeyStoreProvider : SqlColumnEncryptionKeyStoreProvider
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added dummy class to make build pass (not part of snippet)

class Program
{
static void Main()
static void Main(string connectionString)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added missing parameter to make build pass

{
customKeyStoreProviders.Clear();
SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider();
SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(new DefaultAzureCredential());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the updated API to make build pass

}
// </Snippet1>

class MyCustomKeyStoreProvider : SqlColumnEncryptionKeyStoreProvider
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added dummy class to make build pass (not part of snippet)

{
// Create a new SqlColumnEncryptionAzureKeyVaultProvider with the user's credentials and save it for future use
azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider();
azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(new DefaultAzureCredential());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the updated API to make build pass

{
// Create a new SqlColumnEncryptionAzureKeyVaultProvider with the user's credentials and save it for future use
azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider();
azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(new DefaultAzureCredential());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the updated API to make build pass

@@ -1,3 +1,4 @@
/* This does not compile, as multiple methods are missing.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed for build, as this is just code fragments

{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(connection);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed to make build pass

cmd.ExecuteNonQuery();
}
// </Snippet2>
private class RetryCommandSample2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added multiple classes to avoid name clashes

await cmd.ExecuteNonQueryAsync();
}
// </Snippet3>
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added multiple classes to avoid name clashes (outside of snippet)

}

namespace CSDataAdapterOperations.Properties
internal sealed partial class Settings : System.Configuration.ApplicationSettingsBase
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed namespace to make code build.


// Retrieve the ReturnValue.
Int rowCount = (Int)adapter.InsertCommand.Parameters["@RowCount"].Value;
int rowCount = (int)adapter.InsertCommand.Parameters["@RowCount"].Value;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo to make code build

}
}
Console.Writeline($"reader.SensitivityClassification.SensitivityRank : {reader.SensitivityClassification.SensitivityRank.ToString()}");
Console.WriteLine($"reader.SensitivityClassification.SensitivityRank : {reader.SensitivityClassification.SensitivityRank.ToString()}");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo to make code build

using System.Data.Sql;
using System.Data.SqlTypes;
using Microsoft.Data.SqlClient.Server;
using Microsoft.SqlServer.Server;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cheenamalhotra Unsure if this should have been left with the Obsolete classes?

using System.Data;
// <Snippet1>
using Microsoft.Data.SqlClient.Server;
using Microsoft.SqlServer.Server;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cheenamalhotra Unsure if this should have been left with the Obsolete classes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think this was missed out, thanks for fixing it.

using System.Data;
// <Snippet1>
using Microsoft.Data.SqlClient.Server;
using Microsoft.SqlServer.Server;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cheenamalhotra Unsure if this should have been left with the Obsolete classes?

cmd.Parameters.Add(p);

await cmd.PrepareAsync();
cmd.Prepare();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sample build on NetFX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants