Skip to content

Commit

Permalink
ropc fixes for ado.net (#42498)
Browse files Browse the repository at this point in the history
  • Loading branch information
gewarren authored Sep 6, 2024
1 parent 832032f commit cdcd7c4
Show file tree
Hide file tree
Showing 66 changed files with 161 additions and 1,286 deletions.
4 changes: 4 additions & 0 deletions .openpublishing.redirection.framework.json
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@
"redirect_url": "/previous-versions/dotnet/framework/data/adonet/sql/writing-secure-dynamic-sql-in-sql-server",
"redirect_document_id": false
},
{
"source_path_from_root": "/docs/framework/data/adonet/sql/sqldependency-in-an-aspnet-app.md",
"redirect_url": "/dotnet/framework/data/adonet/sql/query-notifications-in-sql-server"
},
{
"source_path_from_root": "/docs/framework/data/wcf/accessing-data-service-resources-wcf-data-services.md",
"redirect_url": "/previous-versions/dotnet/framework/data/wcf/accessing-data-service-resources-wcf-data-services",
Expand Down
6 changes: 5 additions & 1 deletion docs/framework/data/adonet/ado-net-code-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Code examples"
description: "These examples show .NET Framework programmers how to retrieve data from a database by using ADO.NET data providers and ADO.NET Entity Framework."
ms.date: "03/30/2017"
dev_langs:
dev_langs:
- "csharp"
- "vb"
ms.assetid: c119657a-9ce6-4940-91e4-ac1d5f0d9584
Expand Down Expand Up @@ -39,6 +39,8 @@ The following code listings demonstrate how to retrieve data from a database usi

The code in this example assumes that you can connect to the `Northwind` sample database on Microsoft SQL Server. The code creates a <xref:System.Data.SqlClient.SqlCommand> to select rows from the Products table, adding a <xref:System.Data.SqlClient.SqlParameter> to restrict the results to rows with a UnitPrice greater than the specified parameter value, in this case 5. The <xref:System.Data.SqlClient.SqlConnection> is opened inside a `using` block, which ensures that resources are closed and disposed when the code exits. The code executes the command by using a <xref:System.Data.SqlClient.SqlDataReader>, and displays the results in the console window. If you're using `System.Data.SqlClient`, you should consider upgrading to `Microsoft.Data.SqlClient` as it's where future investments and new feature developments are being made. For more information, see [Introducing the new Microsoft.Data.SqlClient](https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient).

[!INCLUDE [managed-identities](../../includes/managed-identities.md)]

[!code-csharp[DataWorks SampleApp.SqlClient#1](../../../../samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks SampleApp.SqlClient/CS/source.cs#1)]
[!code-vb[DataWorks SampleApp.SqlClient#1](../../../../samples/snippets/visualbasic/VS_Snippets_ADO.NET/DataWorks SampleApp.SqlClient/VB/source.vb#1)]

Expand All @@ -60,6 +62,8 @@ The code in this example assumes that you can connect to the Microsoft Access No

The code in this example assumes a connection to DEMO.CUSTOMER on an Oracle server. You must also add a reference to the System.Data.OracleClient.dll. The code returns the data in an <xref:System.Data.OracleClient.OracleDataReader>.

[!INCLUDE [managed-identities](../../includes/managed-identities.md)]

[!code-csharp[DataWorks SampleApp.Oracle#1](../../../../samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks SampleApp.Oracle/CS/source.cs#1)]
[!code-vb[DataWorks SampleApp.Oracle#1](../../../../samples/snippets/visualbasic/VS_Snippets_ADO.NET/DataWorks SampleApp.Oracle/VB/source.vb#1)]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ dev_langs:

Embedding connection strings in your application's code can lead to security vulnerabilities and maintenance problems. Unencrypted connection strings compiled into an application's source code can be viewed using the [Ildasm.exe (IL Disassembler)](../../tools/ildasm-exe-il-disassembler.md) tool. Moreover, if the connection string ever changes, your application must be recompiled. For these reasons, we recommend storing connection strings in an application configuration file.

## Working with Application Configuration Files
[!INCLUDE [managed-identities](../../includes/managed-identities.md)]

## Application Configuration Files

Application configuration files contain settings that are specific to a particular application. For example, an ASP.NET application can have one or more **web.config** files, and a Windows application can have an optional **app.config** file. Configuration files share common elements, although the name and location of a configuration file vary depending on the application's host.

### The connectionStrings Section
### The `connectionStrings` Section

Connection strings can be stored as key/value pairs in the **connectionStrings** section of the **configuration** element of an application configuration file. Child elements include **add**, **clear**, and **remove**.

Expand Down Expand Up @@ -70,7 +72,7 @@ Embedding connection strings in your application's code can lead to security vul
Starting with .NET Framework 2.0, <xref:System.Configuration.ConfigurationManager> is used when working with configuration files on the local computer, replacing the deprecated <xref:System.Configuration.ConfigurationSettings> class. <xref:System.Web.Configuration.WebConfigurationManager> is used to work with ASP.NET configuration files. It is designed to work with configuration files on a Web server, and allows programmatic access to configuration file sections such as **system.web**.

> [!NOTE]
> Accessing configuration files at run time requires granting permissions to the caller; the required permissions depend on the type of application, configuration file, and location. For more information, see [Using the Configuration Classes](/previous-versions/aspnet/ms228063(v=vs.100)) and <xref:System.Web.Configuration.WebConfigurationManager> for ASP.NET applications, and <xref:System.Configuration.ConfigurationManager> for Windows applications.
> Accessing configuration files at run time requires granting permissions to the caller; the required permissions depend on the type of application, configuration file, and location. For more information, see <xref:System.Web.Configuration.WebConfigurationManager> for ASP.NET applications, and <xref:System.Configuration.ConfigurationManager> for Windows applications.
You can use the <xref:System.Configuration.ConnectionStringSettingsCollection> to retrieve connection strings from application configuration files. It contains a collection of <xref:System.Configuration.ConnectionStringSettings> objects, each of which represents a single entry in the **connectionStrings** section. Its properties map to connection string attributes, allowing you to retrieve a connection string by specifying the name or the provider name.

Expand Down Expand Up @@ -106,7 +108,7 @@ Embedding connection strings in your application's code can lead to security vul

## Encrypt Configuration File Sections Using Protected Configuration

ASP.NET 2.0 introduced a new feature, called *protected configuration*, that enables you to encrypt sensitive information in a configuration file. Although primarily designed for ASP.NET, protected configuration can also be used to encrypt configuration file sections in Windows applications. For a detailed description of the protected configuration capabilities, see [Encrypting Configuration Information Using Protected Configuration](/previous-versions/aspnet/53tyfkaw(v=vs.100)).
ASP.NET 2.0 introduced a new feature, called *protected configuration*, that enables you to encrypt sensitive information in a configuration file. Although primarily designed for ASP.NET, protected configuration can also be used to encrypt configuration file sections in Windows applications.

The following configuration file fragment shows the **connectionStrings** section after it has been encrypted. The **configProtectionProvider** specifies the protected configuration provider used to encrypt and decrypt the connection strings. The **EncryptedData** section contains the cipher text.

Expand Down Expand Up @@ -151,19 +153,17 @@ Embedding connection strings in your application's code can lead to security vul
The <xref:System.Configuration> namespace provides classes to work with configuration settings programmatically. The <xref:System.Configuration.ConfigurationManager> class provides access to machine, application, and user configuration files. If you are creating an ASP.NET application, you can use the <xref:System.Web.Configuration.WebConfigurationManager> class, which provides the same functionality while also allowing you to access settings that are unique to ASP.NET applications, such as those found in **\<system.web>**.

> [!NOTE]
> The <xref:System.Security.Cryptography> namespace contains classes that provide additional options for encrypting and decrypting data. Use these classes if you require cryptographic services that are not available using protected configuration. Some of these classes are wrappers for the unmanaged Microsoft CryptoAPI, while others are purely managed implementations. For more information, see [Cryptographic Services](/previous-versions/visualstudio/visual-studio-2008/93bskf9z(v=vs.90)).
> The <xref:System.Security.Cryptography> namespace contains classes that provide additional options for encrypting and decrypting data. Use these classes if you require cryptographic services that are not available using protected configuration. Some of these classes are wrappers for the unmanaged Microsoft CryptoAPI, while others are purely managed implementations.
### App.config Example

This example demonstrates how to toggle encrypting the **connectionStrings** section in an **app.config** file for a Windows application. In this example, the procedure takes the name of the application as an argument, for example, "MyApplication.exe". The **app.config** file is then encrypted and copied to the folder that contains the executable under the name of "MyApplication.exe.config".

> [!NOTE]
> The connection string can only be decrypted on the computer on which it was encrypted.
The code uses the <xref:System.Configuration.ConfigurationManager.OpenExeConfiguration%2A> method to open the **app.config** file for editing, and the <xref:System.Configuration.ConfigurationManager.GetSection%2A> method returns the **connectionStrings** section. The code then checks the <xref:System.Configuration.SectionInformation.IsProtected%2A> property, calling the <xref:System.Configuration.SectionInformation.ProtectSection%2A> to encrypt the section if it is not encrypted. The <xref:System.Configuration.SectionInformation.UnprotectSection%2A> method is invoked to decrypt the section. (The connection string can only be decrypted on the computer on which it was encrypted.) The <xref:System.Configuration.Configuration.Save%2A> method completes the operation and saves the changes.

The code uses the <xref:System.Configuration.ConfigurationManager.OpenExeConfiguration%2A> method to open the **app.config** file for editing, and the <xref:System.Configuration.ConfigurationManager.GetSection%2A> method returns the **connectionStrings** section. The code then checks the <xref:System.Configuration.SectionInformation.IsProtected%2A> property, calling the <xref:System.Configuration.SectionInformation.ProtectSection%2A> to encrypt the section if it is not encrypted. The <xref:System.Configuration.SectionInformation.UnprotectSection%2A> method is invoked to decrypt the section. The <xref:System.Configuration.Configuration.Save%2A> method completes the operation and saves the changes.
You must add a reference to `System.Configuration.dll` in your project for the code to run.

> [!NOTE]
> You must add a reference to `System.Configuration.dll` in your project for the code to run.
[!INCLUDE [managed-identities](../../includes/managed-identities.md)]

[!code-csharp[DataWorks ConnectionStrings.Encrypt#1](../../../../samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks ConnectionStrings.Encrypt/CS/source.cs#1)]
[!code-vb[DataWorks ConnectionStrings.Encrypt#1](../../../../samples/snippets/visualbasic/VS_Snippets_ADO.NET/DataWorks ConnectionStrings.Encrypt/VB/source.vb#1)]
Expand Down
14 changes: 7 additions & 7 deletions docs/framework/data/adonet/obtaining-a-dbproviderfactory.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dev_langs:
The process of obtaining a <xref:System.Data.Common.DbProviderFactory> involves passing information about a data provider to the <xref:System.Data.Common.DbProviderFactories> class. Based on this information, the <xref:System.Data.Common.DbProviderFactories.GetFactory%2A> method creates a strongly typed provider factory. For example, to create a <xref:System.Data.SqlClient.SqlClientFactory>, you can pass `GetFactory` a string with the provider name specified as "System.Data.SqlClient". The other overload of `GetFactory` takes a <xref:System.Data.DataRow>. Once you create the provider factory, you can then use its methods to create additional objects. Some of the methods of a `SqlClientFactory` include <xref:System.Data.SqlClient.SqlClientFactory.CreateConnection%2A>, <xref:System.Data.SqlClient.SqlClientFactory.CreateCommand%2A>, and <xref:System.Data.SqlClient.SqlClientFactory.CreateDataAdapter%2A>.

> [!NOTE]
> The .NET Framework <xref:System.Data.OracleClient.OracleClientFactory>, <xref:System.Data.Odbc.OdbcFactory>, and <xref:System.Data.OleDb.OleDbFactory> classes also provide similar functionality.
> The <xref:System.Data.OracleClient.OracleClientFactory>, <xref:System.Data.Odbc.OdbcFactory>, and <xref:System.Data.OleDb.OleDbFactory> classes also provide similar functionality.
## Registering DbProviderFactories

Expand All @@ -36,12 +36,12 @@ The process of obtaining a <xref:System.Data.Common.DbProviderFactory> involves

You can retrieve information about all of the data providers installed on the local computer by using the <xref:System.Data.Common.DbProviderFactories.GetFactoryClasses%2A> method. It returns a <xref:System.Data.DataTable> named **DbProviderFactories** that contains the columns described in the following table.

|Column ordinal|Column name|Example output|Description|
|--------------------|-----------------|--------------------|-----------------|
|0|**Name**|SqlClient Data Provider|Readable name for the data provider|
|1|**Description**|.Net Framework Data Provider for SqlServer|Readable description of the data provider|
|2|**InvariantName**|System.Data.SqlClient|Name that can be used programmatically to refer to the data provider|
|3|**AssemblyQualifiedName**|System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|Fully qualified name of the factory class, which contains enough information to instantiate the object|
| Column ordinal | Column name | Example output | Description |
|----------------|-----------------|--------------------------------------------|-------------------------------------------|
| 0 | **Name** | SqlClient Data Provider | Readable name for the data provider |
| 1 | **Description** | .Net Framework Data Provider for SqlServer | Readable description of the data provider |
| 2 | **InvariantName** | System.Data.SqlClient | Name that can be used programmatically to refer to the data provider|
| 3 | **AssemblyQualifiedName** | System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 | Fully qualified name of the factory class, which contains enough information to instantiate the object |

This `DataTable` can be used to enable a user to select a <xref:System.Data.DataRow> at run time. The selected `DataRow` can then be passed to the <xref:System.Data.Common.DbProviderFactories.GetFactory%2A> method to create a strongly typed <xref:System.Data.Common.DbProviderFactory>. A selected <xref:System.Data.DataRow> can be passed to the `GetFactory` method to create the desired `DbProviderFactory` object.

Expand Down
Loading

0 comments on commit cdcd7c4

Please sign in to comment.