Refactor and update sample project build.#3816
Conversation
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.
There was a problem hiding this comment.
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 NETFRAMEWORKdirectives - Adds missing
usingdirectives 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 |
doc/samples/Microsoft.SqlServer.Server/csharp/SqlUserDefinedTypeAttribute_Type1.cs
Show resolved
Hide resolved
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
|
"Can you add a comment to this effect? Also, if you use #if false instead of the block comment /* */ then we still get nice IDE presentation and you can put comments about what isn't compiling adjacent to the offending lines." Sure. Will adding #if not disrupt the sample code?? |
|
Basically, for any files that did not have a namespace before, adding it to the top of the file is making sure that this does not interfere with any sample code. |
|
@paulmedynski also not sure the samples are included in any build at the moment... |
|
This pull request has been marked as stale due to inactivity for more than 30 days. If you would like to keep this pull request open, please provide an update or respond to any comments. Otherwise, it will be closed automatically in 7 days. |
|
@paulmedynski Do you have time to respond? |
|
@dotnet/sqlclientdevteam - Can we get some more eyes on this and respond to some of my comments? |
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
|
This pull request has been marked as stale due to inactivity for more than 30 days. If you would like to keep this pull request open, please provide an update or respond to any comments. Otherwise, it will be closed automatically in 7 days. |
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
|
@paulmedynski @cheenamalhotra Comments addressed I stayed on .NET 8 for now, as using .NET 10 caused package downgrade errors This is the resolved version numbers in latest main, curious about 6.1.2 and not 6.1.4 ?
|
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |

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