-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code samples from api reference docs (#196)
- Loading branch information
1 parent
2c487e3
commit d1a2c5f
Showing
37 changed files
with
1,557 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
} |
Oops, something went wrong.