From d40491363bed19880a290790cb740a1cbe85d74e Mon Sep 17 00:00:00 2001
From: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
Date: Wed, 18 Nov 2020 08:25:00 -0800
Subject: [PATCH] Add samples for ADO.NET documentation (#797)
imported documentes from .NET framework.
---
...ectionStringSettings_RetrieveFromConfig.cs | 29 ++++++++++
...StringSettings_RetrieveFromConfigByName.cs | 31 +++++++++++
...ngSettings_RetrieveFromConfigByProvider.cs | 40 ++++++++++++++
doc/samples/ConnectionStringsWeb_Encrypt.cs | 37 +++++++++++++
doc/samples/ConnectionStrings_Encrypt.cs | 48 ++++++++++++++++
doc/samples/SqlCommand_StoredProcedure.cs | 55 +++++++++++++++++++
...ConnectionStringBuilder_InjectionAttack.cs | 20 +++++++
.../SqlConnectionStringBuilder_UserNamePwd.cs | 42 ++++++++++++++
.../SqlConnection_InfoMessage_StateChange.cs | 44 +++++++++++++++
doc/samples/SqlConnection_Pooling.cs | 31 +++++++++++
.../SqlConnection_Pooling_Use_Statement.cs | 21 +++++++
11 files changed, 398 insertions(+)
create mode 100644 doc/samples/ConnectionStringSettings_RetrieveFromConfig.cs
create mode 100644 doc/samples/ConnectionStringSettings_RetrieveFromConfigByName.cs
create mode 100644 doc/samples/ConnectionStringSettings_RetrieveFromConfigByProvider.cs
create mode 100644 doc/samples/ConnectionStringsWeb_Encrypt.cs
create mode 100644 doc/samples/ConnectionStrings_Encrypt.cs
create mode 100644 doc/samples/SqlCommand_StoredProcedure.cs
create mode 100644 doc/samples/SqlConnectionStringBuilder_InjectionAttack.cs
create mode 100644 doc/samples/SqlConnectionStringBuilder_UserNamePwd.cs
create mode 100644 doc/samples/SqlConnection_InfoMessage_StateChange.cs
create mode 100644 doc/samples/SqlConnection_Pooling.cs
create mode 100644 doc/samples/SqlConnection_Pooling_Use_Statement.cs
diff --git a/doc/samples/ConnectionStringSettings_RetrieveFromConfig.cs b/doc/samples/ConnectionStringSettings_RetrieveFromConfig.cs
new file mode 100644
index 0000000000..441537605a
--- /dev/null
+++ b/doc/samples/ConnectionStringSettings_RetrieveFromConfig.cs
@@ -0,0 +1,29 @@
+using System;
+//
+using System.Configuration;
+
+class Program
+{
+ static void Main()
+ {
+ GetConnectionStrings();
+ Console.ReadLine();
+ }
+
+ static void GetConnectionStrings()
+ {
+ ConnectionStringSettingsCollection settings =
+ ConfigurationManager.ConnectionStrings;
+
+ if (settings != null)
+ {
+ foreach(ConnectionStringSettings cs in settings)
+ {
+ Console.WriteLine(cs.Name);
+ Console.WriteLine(cs.ProviderName);
+ Console.WriteLine(cs.ConnectionString);
+ }
+ }
+ }
+}
+//
diff --git a/doc/samples/ConnectionStringSettings_RetrieveFromConfigByName.cs b/doc/samples/ConnectionStringSettings_RetrieveFromConfigByName.cs
new file mode 100644
index 0000000000..1d6109da4c
--- /dev/null
+++ b/doc/samples/ConnectionStringSettings_RetrieveFromConfigByName.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Configuration;
+
+class Program
+{
+ static void Main()
+ {
+ string s = GetConnectionStringByName("NorthwindSQL");
+ Console.WriteLine(s);
+ Console.ReadLine();
+ }
+ //
+ // Retrieves a connection string by name.
+ // Returns null if the name is not found.
+ static string GetConnectionStringByName(string name)
+ {
+ // Assume failure.
+ string returnValue = null;
+
+ // Look for the name in the connectionStrings section.
+ ConnectionStringSettings settings =
+ ConfigurationManager.ConnectionStrings[name];
+
+ // If found, return the connection string.
+ if (settings != null)
+ returnValue = settings.ConnectionString;
+
+ return returnValue;
+ }
+ //
+}
diff --git a/doc/samples/ConnectionStringSettings_RetrieveFromConfigByProvider.cs b/doc/samples/ConnectionStringSettings_RetrieveFromConfigByProvider.cs
new file mode 100644
index 0000000000..89a785d9f7
--- /dev/null
+++ b/doc/samples/ConnectionStringSettings_RetrieveFromConfigByProvider.cs
@@ -0,0 +1,40 @@
+using System;
+using System.Data;
+using Microsoft.Data.SqlClient;
+using System.Configuration;
+
+class Program
+{
+ static void Main()
+ {
+ string s = GetConnectionStringByProvider("Microsoft.Data.SqlClient");
+ Console.WriteLine(s);
+ Console.ReadLine();
+ }
+ //
+ // Retrieve a connection string by specifying the providerName.
+ // Assumes one connection string per provider in the config file.
+ static string GetConnectionStringByProvider(string providerName)
+ {
+ // Return null on failure.
+ string returnValue = null;
+
+ // Get the collection of connection strings.
+ ConnectionStringSettingsCollection settings =
+ ConfigurationManager.ConnectionStrings;
+
+ // Walk through the collection and return the first
+ // connection string matching the providerName.
+ if (settings != null)
+ {
+ foreach (ConnectionStringSettings cs in settings)
+ {
+ if (cs.ProviderName == providerName)
+ returnValue = cs.ConnectionString;
+ break;
+ }
+ }
+ return returnValue;
+ }
+ //
+}
diff --git a/doc/samples/ConnectionStringsWeb_Encrypt.cs b/doc/samples/ConnectionStringsWeb_Encrypt.cs
new file mode 100644
index 0000000000..784d9c1034
--- /dev/null
+++ b/doc/samples/ConnectionStringsWeb_Encrypt.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Configuration;
+using System.Web.Configuration;
+
+class Program
+{
+ static void Main()
+ {
+ }
+ //
+ static void ToggleWebEncrypt()
+ {
+ // Open the Web.config file.
+ Configuration config = WebConfigurationManager.
+ OpenWebConfiguration("~");
+
+ // Get the connectionStrings section.
+ ConnectionStringsSection section =
+ config.GetSection("connectionStrings")
+ as ConnectionStringsSection;
+
+ // Toggle encryption.
+ if (section.SectionInformation.IsProtected)
+ {
+ section.SectionInformation.UnprotectSection();
+ }
+ else
+ {
+ section.SectionInformation.ProtectSection(
+ "DataProtectionConfigurationProvider");
+ }
+
+ // Save changes to the Web.config file.
+ config.Save();
+ }
+ //
+}
diff --git a/doc/samples/ConnectionStrings_Encrypt.cs b/doc/samples/ConnectionStrings_Encrypt.cs
new file mode 100644
index 0000000000..2d47d11327
--- /dev/null
+++ b/doc/samples/ConnectionStrings_Encrypt.cs
@@ -0,0 +1,48 @@
+using System;
+using System.Configuration;
+
+class Program
+{
+ static void Main()
+ {
+ }
+ //
+ static void ToggleConfigEncryption(string exeFile)
+ {
+ // Takes the executable file name without the
+ // .config extension.
+ try
+ {
+ // Open the configuration file and retrieve
+ // the connectionStrings section.
+ Configuration config = ConfigurationManager.
+ OpenExeConfiguration(exeConfigName);
+
+ ConnectionStringsSection section =
+ config.GetSection("connectionStrings")
+ as ConnectionStringsSection;
+
+ if (section.SectionInformation.IsProtected)
+ {
+ // Remove encryption.
+ section.SectionInformation.UnprotectSection();
+ }
+ else
+ {
+ // Encrypt the section.
+ section.SectionInformation.ProtectSection(
+ "DataProtectionConfigurationProvider");
+ }
+ // Save the current configuration.
+ config.Save();
+
+ Console.WriteLine("Protected={0}",
+ section.SectionInformation.IsProtected);
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ }
+ //
+}
diff --git a/doc/samples/SqlCommand_StoredProcedure.cs b/doc/samples/SqlCommand_StoredProcedure.cs
new file mode 100644
index 0000000000..2b9ef3e048
--- /dev/null
+++ b/doc/samples/SqlCommand_StoredProcedure.cs
@@ -0,0 +1,55 @@
+using System;
+using System.Data;
+using Microsoft.Data.SqlClient;
+class Program
+{
+ static void Main()
+ {
+ // Supply any valid Document ID value.
+ // The value 7 is supplied for demonstration purposes.
+ GetSalesByCategory("Data Source=(local);Initial Catalog=Northwind;Integrated Security=true;", "Confections");
+ Console.ReadLine();
+ }
+ //
+ static void GetSalesByCategory(string connectionString,
+ string categoryName)
+ {
+ using (SqlConnection connection = new SqlConnection(connectionString))
+ {
+ // Create the command and set its properties.
+ SqlCommand command = new SqlCommand();
+ command.Connection = connection;
+ command.CommandText = "SalesByCategory";
+ command.CommandType = CommandType.StoredProcedure;
+
+ // Add the input parameter and set its properties.
+ SqlParameter parameter = new SqlParameter();
+ parameter.ParameterName = "@CategoryName";
+ parameter.SqlDbType = SqlDbType.NVarChar;
+ parameter.Direction = ParameterDirection.Input;
+ parameter.Value = categoryName;
+
+ // Add the parameter to the Parameters collection.
+ command.Parameters.Add(parameter);
+
+ // Open the connection and execute the reader.
+ connection.Open();
+ using (SqlDataReader reader = command.ExecuteReader())
+ {
+ if (reader.HasRows)
+ {
+ while (reader.Read())
+ {
+ Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
+ }
+ }
+ else
+ {
+ Console.WriteLine("No rows found.");
+ }
+ reader.Close();
+ }
+ }
+ }
+ //
+}
diff --git a/doc/samples/SqlConnectionStringBuilder_InjectionAttack.cs b/doc/samples/SqlConnectionStringBuilder_InjectionAttack.cs
new file mode 100644
index 0000000000..e91c629ac4
--- /dev/null
+++ b/doc/samples/SqlConnectionStringBuilder_InjectionAttack.cs
@@ -0,0 +1,20 @@
+using System;
+using System.Data;
+using Microsoft.Data.SqlClient;
+
+class Program
+{
+ static void Main()
+ {
+ //
+ SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
+ builder.DataSource = "(local)";
+ builder.IntegratedSecurity = true;
+ builder.InitialCatalog = "AdventureWorks;NewValue=Bad";
+ Console.WriteLine(builder.ConnectionString);
+ //
+
+ Console.WriteLine("Press Enter to continue.");
+ Console.ReadLine();
+ }
+}
diff --git a/doc/samples/SqlConnectionStringBuilder_UserNamePwd.cs b/doc/samples/SqlConnectionStringBuilder_UserNamePwd.cs
new file mode 100644
index 0000000000..efd4455c1b
--- /dev/null
+++ b/doc/samples/SqlConnectionStringBuilder_UserNamePwd.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Data;
+using Microsoft.Data.SqlClient;
+using System.Configuration;
+
+class Program
+{
+ static void Main()
+ {
+ BuildConnectionString("(local)", "Nate", "somepassword");
+ Console.ReadLine();
+ }
+
+ //
+ private static void BuildConnectionString(string dataSource,
+ string userName, string userPassword)
+ {
+ // Retrieve the partial connection string named databaseConnection
+ // from the application's app.config or web.config file.
+ ConnectionStringSettings settings =
+ ConfigurationManager.ConnectionStrings["partialConnectString"];
+
+ if (null != settings)
+ {
+ // Retrieve the partial connection string.
+ string connectString = settings.ConnectionString;
+ Console.WriteLine("Original: {0}", connectString);
+
+ // Create a new SqlConnectionStringBuilder based on the
+ // partial connection string retrieved from the config file.
+ SqlConnectionStringBuilder builder =
+ new SqlConnectionStringBuilder(connectString);
+
+ // Supply the additional values.
+ builder.DataSource = dataSource;
+ builder.UserID = userName;
+ builder.Password = userPassword;
+ Console.WriteLine("Modified: {0}", builder.ConnectionString);
+ }
+ }
+ //
+}
diff --git a/doc/samples/SqlConnection_InfoMessage_StateChange.cs b/doc/samples/SqlConnection_InfoMessage_StateChange.cs
new file mode 100644
index 0000000000..06e7ec0578
--- /dev/null
+++ b/doc/samples/SqlConnection_InfoMessage_StateChange.cs
@@ -0,0 +1,44 @@
+using System;
+using System.Data;
+using Microsoft.Data.SqlClient;
+
+namespace ClassLibraryTest
+{
+ public class Class1
+ {
+ public SqlConnection InitiateConnection(string connectionString)
+ {
+ // Assumes that connectionString is a valid SQL Server connection string.
+ var connection = new SqlConnection(connectionString);
+
+ //
+ // Assumes that connection represents a SqlConnection object.
+ connection.InfoMessage +=
+ (object sender, SqlInfoMessageEventArgs args) =>
+ {
+ foreach (SqlError err in args.Errors)
+ {
+ Console.WriteLine(
+ "The {0} has received a severity {1}, state {2} error number {3}\n" +
+ "on line {4} of procedure {5} on server {6}:\n{7}",
+ err.Source, err.Class, err.State, err.Number, err.LineNumber,
+ err.Procedure, err.Server, err.Message);
+ }
+ };
+ //
+
+ //
+ // Assumes that connection represents a SqlConnection object.
+ connection.StateChange +=
+ (object sender, StateChangeEventArgs args) =>
+ {
+ Console.WriteLine(
+ "The current Connection state has changed from {0} to {1}.",
+ args.OriginalState, args.CurrentState);
+ };
+ //
+
+ return connection;
+ }
+ }
+}
diff --git a/doc/samples/SqlConnection_Pooling.cs b/doc/samples/SqlConnection_Pooling.cs
new file mode 100644
index 0000000000..f3a0522c55
--- /dev/null
+++ b/doc/samples/SqlConnection_Pooling.cs
@@ -0,0 +1,31 @@
+using System;
+using Microsoft.Data.SqlClient;
+
+class Program
+{
+ static void Main()
+ {
+ //
+ using (SqlConnection connection = new SqlConnection(
+ "Integrated Security=SSPI;Initial Catalog=Northwind"))
+ {
+ connection.Open();
+ // Pool A is created.
+ }
+
+ using (SqlConnection connection = new SqlConnection(
+ "Integrated Security=SSPI;Initial Catalog=pubs"))
+ {
+ connection.Open();
+ // Pool B is created because the connection strings differ.
+ }
+
+ using (SqlConnection connection = new SqlConnection(
+ "Integrated Security=SSPI;Initial Catalog=Northwind"))
+ {
+ connection.Open();
+ // The connection string matches pool A.
+ }
+ //
+ }
+}
diff --git a/doc/samples/SqlConnection_Pooling_Use_Statement.cs b/doc/samples/SqlConnection_Pooling_Use_Statement.cs
new file mode 100644
index 0000000000..daadeea1e0
--- /dev/null
+++ b/doc/samples/SqlConnection_Pooling_Use_Statement.cs
@@ -0,0 +1,21 @@
+using System;
+using Microsoft.Data.SqlClient;
+
+class Program
+{
+ static void Main()
+ {
+ string connectionString = "Server=localhost;Database=master;Integrated Security=true;"
+ //
+ // Assume that connectionString connects to master.
+ using (SqlConnection connection = new SqlConnection(connectionString))
+ using (SqlCommand command = new SqlCommand())
+ {
+ connection.Open();
+ command.Connection = connection;
+ command.Text = "USE DatabaseName";
+ command.ExecuteNonQuery();
+ }
+ //
+ }
+}