Skip to content

Commit

Permalink
Add code samples from api reference docs (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
yukiwongky authored and cheenamalhotra committed Sep 13, 2019
1 parent 2c487e3 commit d1a2c5f
Show file tree
Hide file tree
Showing 37 changed files with 1,557 additions and 0 deletions.
73 changes: 73 additions & 0 deletions doc/samples/DataTableReader_GetValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Data;
using Microsoft.Data.SqlClient;

class Program
{
static void Main()
{
}
// <Snippet1>
private static void TestGetValues(DataTableReader reader)
{
// Given a DataTableReader, use the GetValues
// method to retrieve a full row of data.
// Test the GetValues method, passing in an array large
// enough for all the columns.
Object[] values = new Object[reader.FieldCount];
int fieldCount = reader.GetValues(values);

Console.WriteLine("reader.GetValues retrieved {0} columns.",
fieldCount);
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(values[i]);

Console.WriteLine();

// Now repeat, using an array that may contain a different
// number of columns than the original data. This should work correctly,
// whether the size of the array is larger or smaller than
// the number of columns.

// Attempt to retrieve three columns of data.
values = new Object[3];
fieldCount = reader.GetValues(values);
Console.WriteLine("reader.GetValues retrieved {0} columns.",
fieldCount);
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(values[i]);
}
// </Snippet1>

// <Snippet2>
private static void TestGetValues(SqlDataReader reader)
{
// Given a SqlDataReader, use the GetValues
// method to retrieve a full row of data.
// Test the GetValues method, passing in an array large
// enough for all the columns.
Object[] values = new Object[reader.FieldCount];
int fieldCount = reader.GetValues(values);

Console.WriteLine("reader.GetValues retrieved {0} columns.",
fieldCount);
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(values[i]);

Console.WriteLine();

// Now repeat, using an array that may contain a different
// number of columns than the original data. This should work correctly,
// whether the size of the array is larger or smaller than
// the number of columns.

// Attempt to retrieve three columns of data.
values = new Object[3];
fieldCount = reader.GetValues(values);
Console.WriteLine("reader.GetValues retrieved {0} columns.",
fieldCount);
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(values[i]);
}
// </Snippet2>
}
71 changes: 71 additions & 0 deletions doc/samples/SqlConnection_BeginTransaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Data;
using Microsoft.Data.SqlClient;


namespace Transaction1CS
{
class Program
{
static void Main()
{
string connectionString =
"Persist Security Info=False;Integrated Security=SSPI;database=Northwind;server=(local)";
ExecuteSqlTransaction(connectionString);
Console.ReadLine();
}
// <Snippet1>
private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;

// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");

// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;

try
{
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery();

// Attempt to commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);

// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
// </Snippet1>
}
}
71 changes: 71 additions & 0 deletions doc/samples/SqlConnection_BeginTransaction2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Data;
using Microsoft.Data.SqlClient;


namespace Transaction1CS
{
class Program
{
static void Main()
{
string connectionString =
"Persist Security Info=False;Integrated Security=SSPI;database=Northwind;server=(local)";
ExecuteSqlTransaction(connectionString);
Console.ReadLine();
}
// <Snippet1>
private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;

// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");

// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;

try
{
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery();

// Attempt to commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);

// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
// </Snippet1>
}
}
66 changes: 66 additions & 0 deletions doc/samples/SqlDataAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Data;
using Microsoft.Data.SqlClient;


class Program
{
static void Main()
{
}
//<Snippet1>
public static SqlDataAdapter CreateCustomerAdapter(
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();

// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection);

// Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15);

adapter.SelectCommand = command;

// Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);

// Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

adapter.InsertCommand = command;

// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);

// Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;

adapter.UpdateCommand = command;

// Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;

adapter.DeleteCommand = command;

return adapter;
}
//</Snippet1>
}
79 changes: 79 additions & 0 deletions doc/samples/SqlDataAdapter_RowUpdated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Xml;
using System.Data;
using Microsoft.Data.SqlClient;
using System.Data.Common;
using System.Windows.Forms;

public class Form1: Form
{
private DataSet DataSet1;
private DataGrid dataGrid1;


// <Snippet1>
// handler for RowUpdating event
private static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
PrintEventArgs(e);
}

// handler for RowUpdated event
private static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
PrintEventArgs(e);
}

public static int Main()
{
const string connectionString =
"Integrated Security=SSPI;database=Northwind;server=MSSQL1";
const string queryString = "SELECT * FROMProducts";

// create DataAdapter
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connectionString);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

// Create and fill DataSet (select only first 5 rows)
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, 0, 5, "Table");

// Modify DataSet
DataTable table = dataSet.Tables["Table"];
table.Rows[0][1] = "new product";

// add handlers
adapter.RowUpdating += new SqlRowUpdatingEventHandler( OnRowUpdating );
adapter.RowUpdated += new SqlRowUpdatedEventHandler( OnRowUpdated );

// update, this operation fires two events
// (RowUpdating/RowUpdated) per changed row
adapter.Update(dataSet, "Table");

// remove handlers
adapter.RowUpdating -= new SqlRowUpdatingEventHandler( OnRowUpdating );
adapter.RowUpdated -= new SqlRowUpdatedEventHandler( OnRowUpdated );
return 0;
}

private static void PrintEventArgs(SqlRowUpdatingEventArgs args)
{
Console.WriteLine("OnRowUpdating");
Console.WriteLine(" event args: ("+
" command=" + args.Command +
" commandType=" + args.StatementType +
" status=" + args.Status + ")");
}

private static void PrintEventArgs(SqlRowUpdatedEventArgs args)
{
Console.WriteLine("OnRowUpdated");
Console.WriteLine( " event args: ("+
" command=" + args.Command +
" commandType=" + args.StatementType +
" recordsAffected=" + args.RecordsAffected +
" status=" + args.Status + ")");
}
// </Snippet1>

}
Loading

0 comments on commit d1a2c5f

Please sign in to comment.