diff --git a/doc/samples/DataTableReader_GetValues.cs b/doc/samples/DataTableReader_GetValues.cs new file mode 100644 index 0000000000..f870e292e9 --- /dev/null +++ b/doc/samples/DataTableReader_GetValues.cs @@ -0,0 +1,73 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + 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]); + } + // + + // + 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]); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlConnection_BeginTransaction.cs b/doc/samples/SqlConnection_BeginTransaction.cs new file mode 100644 index 0000000000..9e5fa0bcf2 --- /dev/null +++ b/doc/samples/SqlConnection_BeginTransaction.cs @@ -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(); + } + // + 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); + } + } + } + } + // + } +} \ No newline at end of file diff --git a/doc/samples/SqlConnection_BeginTransaction2.cs b/doc/samples/SqlConnection_BeginTransaction2.cs new file mode 100644 index 0000000000..9e5fa0bcf2 --- /dev/null +++ b/doc/samples/SqlConnection_BeginTransaction2.cs @@ -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(); + } + // + 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); + } + } + } + } + // + } +} \ No newline at end of file diff --git a/doc/samples/SqlDataAdapter.cs b/doc/samples/SqlDataAdapter.cs new file mode 100644 index 0000000000..5d14af99c3 --- /dev/null +++ b/doc/samples/SqlDataAdapter.cs @@ -0,0 +1,66 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + } + // + 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; + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlDataAdapter_RowUpdated.cs b/doc/samples/SqlDataAdapter_RowUpdated.cs new file mode 100644 index 0000000000..173f77cadb --- /dev/null +++ b/doc/samples/SqlDataAdapter_RowUpdated.cs @@ -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; + + + // + // 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 + ")"); + } + // + +} \ No newline at end of file diff --git a/doc/samples/SqlDataAdapter_SelectCommand.cs b/doc/samples/SqlDataAdapter_SelectCommand.cs new file mode 100644 index 0000000000..aa8dea3f5c --- /dev/null +++ b/doc/samples/SqlDataAdapter_SelectCommand.cs @@ -0,0 +1,31 @@ +using System; +using System.Xml; +using System.Data; +using Microsoft.Data.SqlClient; +using System.Data.Common; +using System.Windows.Forms; + +public class Form1: Form +{ + protected DataSet DataSet1; + protected DataGrid dataGrid1; + + +// +private static DataSet SelectRows(DataSet dataset, + string connectionString,string queryString) +{ + using (SqlConnection connection = + new SqlConnection(connectionString)) + { + SqlDataAdapter adapter = new SqlDataAdapter(); + adapter.SelectCommand = new SqlCommand( + queryString, connection); + adapter.Fill(dataset); + return dataset; + } +} + +// + +} \ No newline at end of file diff --git a/doc/samples/SqlDataAdapter_SqlDataAdapter.cs b/doc/samples/SqlDataAdapter_SqlDataAdapter.cs new file mode 100644 index 0000000000..13fd2e590c --- /dev/null +++ b/doc/samples/SqlDataAdapter_SqlDataAdapter.cs @@ -0,0 +1,50 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + } + // + public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection) + { + SqlDataAdapter adapter = new SqlDataAdapter(); + adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; + + // Create the commands. + adapter.SelectCommand = new SqlCommand( + "SELECT CustomerID, CompanyName FROM CUSTOMERS", connection); + adapter.InsertCommand = new SqlCommand( + "INSERT INTO Customers (CustomerID, CompanyName) " + + "VALUES (@CustomerID, @CompanyName)", connection); + adapter.UpdateCommand = new SqlCommand( + "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " + + "WHERE CustomerID = @oldCustomerID", connection); + adapter.DeleteCommand = new SqlCommand( + "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); + + // Create the parameters. + adapter.InsertCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID"); + adapter.InsertCommand.Parameters.Add("@CompanyName", + SqlDbType.VarChar, 40, "CompanyName"); + + adapter.UpdateCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID"); + adapter.UpdateCommand.Parameters.Add("@CompanyName", + SqlDbType.VarChar, 40, "CompanyName"); + adapter.UpdateCommand.Parameters.Add("@oldCustomerID", + SqlDbType.Char, 5, "CustomerID").SourceVersion = + DataRowVersion.Original; + + adapter.DeleteCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID").SourceVersion = + DataRowVersion.Original; + + return adapter; + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlDataAdapter_SqlDataAdapter1.cs b/doc/samples/SqlDataAdapter_SqlDataAdapter1.cs new file mode 100644 index 0000000000..5e5647b54d --- /dev/null +++ b/doc/samples/SqlDataAdapter_SqlDataAdapter1.cs @@ -0,0 +1,49 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + } + // + public static SqlDataAdapter CreateSqlDataAdapter(SqlCommand selectCommand, + SqlConnection connection) + { + SqlDataAdapter adapter = new SqlDataAdapter(selectCommand); + adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; + + // Create the other commands. + adapter.InsertCommand = new SqlCommand( + "INSERT INTO Customers (CustomerID, CompanyName) " + + "VALUES (@CustomerID, @CompanyName)", connection); + + adapter.UpdateCommand = new SqlCommand( + "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " + + "WHERE CustomerID = @oldCustomerID", connection); + + adapter.DeleteCommand = new SqlCommand( + "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); + + // Create the parameters. + adapter.InsertCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID"); + adapter.InsertCommand.Parameters.Add("@CompanyName", + SqlDbType.VarChar, 40, "CompanyName"); + + adapter.UpdateCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID"); + adapter.UpdateCommand.Parameters.Add("@CompanyName", + SqlDbType.VarChar, 40, "CompanyName"); + adapter.UpdateCommand.Parameters.Add("@oldCustomerID", + SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; + + adapter.DeleteCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; + + return adapter; + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlDataAdapter_SqlDataAdapter2.cs b/doc/samples/SqlDataAdapter_SqlDataAdapter2.cs new file mode 100644 index 0000000000..9400915351 --- /dev/null +++ b/doc/samples/SqlDataAdapter_SqlDataAdapter2.cs @@ -0,0 +1,51 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + } + // + public static SqlDataAdapter CreateSqlDataAdapter(string commandText, + string connectionString) + { + SqlDataAdapter adapter = new SqlDataAdapter(commandText, connectionString); + SqlConnection connection = adapter.SelectCommand.Connection; + + adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; + + // Create the commands. + adapter.InsertCommand = new SqlCommand( + "INSERT INTO Customers (CustomerID, CompanyName) " + + "VALUES (@CustomerID, @CompanyName)", connection); + + adapter.UpdateCommand = new SqlCommand( + "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " + + "WHERE CustomerID = @oldCustomerID", connection); + + adapter.DeleteCommand = new SqlCommand( + "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); + + // Create the parameters. + adapter.InsertCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID"); + adapter.InsertCommand.Parameters.Add("@CompanyName", + SqlDbType.VarChar, 40, "CompanyName"); + + adapter.UpdateCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID"); + adapter.UpdateCommand.Parameters.Add("@CompanyName", + SqlDbType.VarChar, 40, "CompanyName"); + adapter.UpdateCommand.Parameters.Add("@oldCustomerID", + SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; + + adapter.DeleteCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; + + return adapter; + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlDataAdapter_SqlDataAdapter3.cs b/doc/samples/SqlDataAdapter_SqlDataAdapter3.cs new file mode 100644 index 0000000000..24498a147a --- /dev/null +++ b/doc/samples/SqlDataAdapter_SqlDataAdapter3.cs @@ -0,0 +1,50 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + } + // + public static SqlDataAdapter CreateSqlDataAdapter(string commandText, + SqlConnection connection) + { + SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection); + + adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; + + // Create the other commands. + adapter.InsertCommand = new SqlCommand( + "INSERT INTO Customers (CustomerID, CompanyName) " + + "VALUES (@CustomerID, @CompanyName)"); + + adapter.UpdateCommand = new SqlCommand( + "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " + + "WHERE CustomerID = @oldCustomerID"); + + adapter.DeleteCommand = new SqlCommand( + "DELETE FROM Customers WHERE CustomerID = @CustomerID"); + + // Create the parameters. + adapter.InsertCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID"); + adapter.InsertCommand.Parameters.Add("@CompanyName", + SqlDbType.VarChar, 40, "CompanyName"); + + adapter.UpdateCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID"); + adapter.UpdateCommand.Parameters.Add("@CompanyName", + SqlDbType.VarChar, 40, "CompanyName"); + adapter.UpdateCommand.Parameters.Add("@oldCustomerID", + SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; + + adapter.DeleteCommand.Parameters.Add("@CustomerID", + SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; + + return adapter; + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlDataReader_Close.cs b/doc/samples/SqlDataReader_Close.cs new file mode 100644 index 0000000000..44154e0f5f --- /dev/null +++ b/doc/samples/SqlDataReader_Close.cs @@ -0,0 +1,44 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + string str = "Data Source=(local);Initial Catalog=Northwind;" + + "Integrated Security=SSPI"; + ReadOrderData(str); + } + // + private static void ReadOrderData(string connectionString) + { + string queryString = + "SELECT OrderID, CustomerID FROM dbo.Orders;"; + + using (SqlConnection connection = + new SqlConnection(connectionString)) + { + connection.Open(); + + using (SqlCommand command = + new SqlCommand(queryString, connection)) + { + using (SqlDataReader reader = command.ExecuteReader()) + { + // Call Read before accessing data. + while (reader.Read()) + { + Console.WriteLine(String.Format("{0}, {1}", + reader[0], reader[1])); + } + + // Call Close when done reading. + reader.Close(); + } + } + } + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlDataReader_GetOrdinal.cs b/doc/samples/SqlDataReader_GetOrdinal.cs new file mode 100644 index 0000000000..a5af612d1f --- /dev/null +++ b/doc/samples/SqlDataReader_GetOrdinal.cs @@ -0,0 +1,43 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + string str = "Data Source=(local);Initial Catalog=Northwind;" + + "Integrated Security=SSPI"; + ReadGetOrdinal(str); + } + + // + private static void ReadGetOrdinal(string connectionString) + { + string queryString = "SELECT DISTINCT CustomerID FROM dbo.Orders;"; + using (SqlConnection connection = + new SqlConnection(connectionString)) + { + SqlCommand command = + new SqlCommand(queryString, connection); + connection.Open(); + + SqlDataReader reader = command.ExecuteReader(); + + // Call GetOrdinal and assign value to variable. + int customerID = reader.GetOrdinal("CustomerID"); + + // Use variable with GetString inside of loop. + while (reader.Read()) + { + Console.WriteLine("CustomerID={0}", reader.GetString(customerID)); + } + + // Call Close when done reading. + reader.Close(); + } + } + // + +} \ No newline at end of file diff --git a/doc/samples/SqlDataReader_IsDBNull.cs b/doc/samples/SqlDataReader_IsDBNull.cs new file mode 100644 index 0000000000..28faf0395d --- /dev/null +++ b/doc/samples/SqlDataReader_IsDBNull.cs @@ -0,0 +1,25 @@ +// +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program { + static void Main(string[] args) { + + using (var connection = new SqlConnection(@"Data Source=(local);Initial Catalog=AdventureWorks2012;Integrated Security=SSPI")) { + var command = new SqlCommand("SELECT p.FirstName, p.MiddleName, p.LastName FROM HumanResources.Employee AS e" + + " JOIN Person.Person AS p ON e.BusinessEntityID = p.BusinessEntityID;", connection); + connection.Open(); + var reader = command.ExecuteReader(); + while (reader.Read()) { + Console.Write(reader.GetString(reader.GetOrdinal("FirstName"))); + // display middle name only of not null + if (!reader.IsDBNull(reader.GetOrdinal("MiddleName"))) + Console.Write(" {0}", reader.GetString(reader.GetOrdinal("MiddleName"))); + Console.WriteLine(" {0}", reader.GetString(reader.GetOrdinal("LastName"))); + } + connection.Close(); + } + } +} +// \ No newline at end of file diff --git a/doc/samples/SqlDataReader_Read.cs b/doc/samples/SqlDataReader_Read.cs new file mode 100644 index 0000000000..d7a9a40df3 --- /dev/null +++ b/doc/samples/SqlDataReader_Read.cs @@ -0,0 +1,47 @@ +// +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + string str = "Data Source=(local);Initial Catalog=Northwind;" + + "Integrated Security=SSPI"; + ReadOrderData(str); + } + + private static void ReadOrderData(string connectionString) + { + string queryString = + "SELECT OrderID, CustomerID FROM dbo.Orders;"; + + using (SqlConnection connection = + new SqlConnection(connectionString)) + { + SqlCommand command = + new SqlCommand(queryString, connection); + connection.Open(); + + SqlDataReader reader = command.ExecuteReader(); + + // Call Read before accessing data. + while (reader.Read()) + { + ReadSingleRow((IDataRecord)reader); + } + + // Call Close when done reading. + reader.Close(); + } + } + + private static void ReadSingleRow(IDataRecord record) + { + Console.WriteLine(String.Format("{0}, {1}", record[0], record[1])); + } + +} +// \ No newline at end of file diff --git a/doc/samples/SqlError_State.cs b/doc/samples/SqlError_State.cs new file mode 100644 index 0000000000..c6482c9ea0 --- /dev/null +++ b/doc/samples/SqlError_State.cs @@ -0,0 +1,35 @@ +using System; +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Data.SqlClient; + +namespace Classic_WebData_SqlError.StateCS +{ + class Program + { + static void Main() + { + //DisplaySqlErrors(); + } + + // + public void DisplaySqlErrors(SqlException exception) + { + for (int i = 0; i < exception.Errors.Count; i++) + { + Console.WriteLine("Index #" + i + "\n" + + "Source: " + exception.Errors[i].Source + "\n" + + "Number: " + exception.Errors[i].Number.ToString() + "\n" + + "State: " + exception.Errors[i].State.ToString() + "\n" + + "Class: " + exception.Errors[i].Class.ToString() + "\n" + + "Server: " + exception.Errors[i].Server + "\n" + + "Message: " + exception.Errors[i].Message + "\n" + + "Procedure: " + exception.Errors[i].Procedure + "\n" + + "LineNumber: " + exception.Errors[i].LineNumber.ToString()); + } + Console.ReadLine(); + } + // + } +} \ No newline at end of file diff --git a/doc/samples/SqlError_ToString.cs b/doc/samples/SqlError_ToString.cs new file mode 100644 index 0000000000..4f22a76427 --- /dev/null +++ b/doc/samples/SqlError_ToString.cs @@ -0,0 +1,53 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; +using System.Text; + +class Program +{ + static void Main() + { + string s = GetConnectionString(); + ShowSqlException(s); + Console.ReadLine(); + } + // + public static void ShowSqlException(string connectionString) + { + string queryString = "EXECUTE NonExistantStoredProcedure"; + + using (SqlConnection connection = new SqlConnection(connectionString)) + { + SqlCommand command = new SqlCommand(queryString, connection); + try + { + command.Connection.Open(); + command.ExecuteNonQuery(); + } + catch (SqlException ex) + { + DisplaySqlErrors(ex); + } + } + } + + private static void DisplaySqlErrors(SqlException exception) + { + for (int i = 0; i < exception.Errors.Count; i++) + { + Console.WriteLine("Index #" + i + "\n" + + "Error: " + exception.Errors[i].ToString() + "\n"); + } + Console.ReadLine(); + } + // + + + static private string GetConnectionString() + { + // To avoid storing the connection string in your code, + // you can retrieve it from a configuration file. + return "Data Source=(local);Initial Catalog=AdventureWorks;" + + "Integrated Security=SSPI"; + } +} \ No newline at end of file diff --git a/doc/samples/SqlException_Errors1.cs b/doc/samples/SqlException_Errors1.cs new file mode 100644 index 0000000000..59b35c6a42 --- /dev/null +++ b/doc/samples/SqlException_Errors1.cs @@ -0,0 +1,51 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; +using System.Text; + +class Program +{ + static void Main() + { + string s = GetConnectionString(); + ShowSqlException(s); + Console.ReadLine(); + } + // + public static void ShowSqlException(string connectionString) + { + string queryString = "EXECUTE NonExistantStoredProcedure"; + StringBuilder errorMessages = new StringBuilder(); + + using (SqlConnection connection = new SqlConnection(connectionString)) + { + SqlCommand command = new SqlCommand(queryString, connection); + try + { + command.Connection.Open(); + command.ExecuteNonQuery(); + } + catch (SqlException ex) + { + for (int i = 0; i < ex.Errors.Count; i++) + { + errorMessages.Append("Index #" + i + "\n" + + "Message: " + ex.Errors[i].Message + "\n" + + "LineNumber: " + ex.Errors[i].LineNumber + "\n" + + "Source: " + ex.Errors[i].Source + "\n" + + "Procedure: " + ex.Errors[i].Procedure + "\n"); + } + Console.WriteLine(errorMessages.ToString()); + } + } + } + // + + static private string GetConnectionString() + { + // To avoid storing the connection string in your code, + // you can retrieve it from a configuration file. + return "Data Source=(local);Initial Catalog=AdventureWorks;" + + "Integrated Security=SSPI"; + } +} \ No newline at end of file diff --git a/doc/samples/SqlException_Errors2.cs b/doc/samples/SqlException_Errors2.cs new file mode 100644 index 0000000000..16601d4ed7 --- /dev/null +++ b/doc/samples/SqlException_Errors2.cs @@ -0,0 +1,53 @@ +// +using System; +using System.Data; +using Microsoft.Data.SqlClient; +using System.Text; + +class Program +{ + static void Main() + { + string s = GetConnectionString(); + ShowSqlException(s); + Console.ReadLine(); + } + + public static void ShowSqlException(string connectionString) + { + string queryString = "EXECUTE NonExistantStoredProcedure"; + StringBuilder errorMessages = new StringBuilder(); + + using (SqlConnection connection = new SqlConnection(connectionString)) + { + SqlCommand command = new SqlCommand(queryString, connection); + try + { + command.Connection.Open(); + command.ExecuteNonQuery(); + } + catch (SqlException ex) + { + for (int i = 0; i < ex.Errors.Count; i++) + { + errorMessages.Append("Index #" + i + "\n" + + "Message: " + ex.Errors[i].Message + "\n" + + "Error Number: " + ex.Errors[i].Number + "\n" + + "LineNumber: " + ex.Errors[i].LineNumber + "\n" + + "Source: " + ex.Errors[i].Source + "\n" + + "Procedure: " + ex.Errors[i].Procedure + "\n"); + } + Console.WriteLine(errorMessages.ToString()); + } + } + } + + static private string GetConnectionString() + { + // To avoid storing the connection string in your code, + // you can retrieve it from a configuration file. + return "Data Source=(local);Initial Catalog=AdventureWorks;" + + "Integrated Security=SSPI"; + } +} +// \ No newline at end of file diff --git a/doc/samples/SqlParameter.cs b/doc/samples/SqlParameter.cs new file mode 100644 index 0000000000..22899a8a83 --- /dev/null +++ b/doc/samples/SqlParameter.cs @@ -0,0 +1,84 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + } + + // + static void CreateSqlParameterOutput() + { + SqlParameter parameter = new SqlParameter("Description", SqlDbType.VarChar, 88); + parameter.Direction = ParameterDirection.Output; + } + // + + // + static void CreateSqlParameterNullable() + { + SqlParameter parameter = new SqlParameter("Description", SqlDbType.VarChar, 88); + parameter.IsNullable = true; + parameter.Direction = ParameterDirection.Output; + } + // + + // + static void CreateSqlParameterOffset() + { + SqlParameter parameter = new SqlParameter("pDName", SqlDbType.VarChar); + parameter.IsNullable = true; + parameter.Offset = 3; + } + // + + // + static void CreateSqlParameterPrecisionScale() + { + SqlParameter parameter = new SqlParameter("Price", SqlDbType.Decimal); + parameter.Value = 3.1416; + parameter.Precision = 8; + parameter.Scale = 4; + } + // + + // + static void CreateSqlParameterSize() + { + string description = "12 foot scarf - multiple colors, one previous owner"; + SqlParameter parameter = new SqlParameter("Description", SqlDbType.VarChar); + parameter.Direction = ParameterDirection.InputOutput; + parameter.Size = description.Length; + parameter.Value = description; + } + // + + // + static void CreateSqlParameterSourceColumn() + { + SqlParameter parameter = new SqlParameter("Description", SqlDbType.VarChar, 88); + parameter.SourceColumn = "Description"; + } + // + + // + static void CreateSqlParameterSourceVersion() + { + SqlParameter parameter = new SqlParameter("Description", SqlDbType.VarChar, 88); + parameter.SourceColumn = "Description"; + parameter.SourceVersion = DataRowVersion.Current; + } + // + + // + static void CreateSqlParameterVersion() + { + SqlParameter parameter = new SqlParameter("Description", SqlDbType.VarChar, 88); + parameter.Value = "garden hose"; + } + // + +} \ No newline at end of file diff --git a/doc/samples/SqlParameterCollection_Add.cs b/doc/samples/SqlParameterCollection_Add.cs new file mode 100644 index 0000000000..4007715b0a --- /dev/null +++ b/doc/samples/SqlParameterCollection_Add.cs @@ -0,0 +1,13 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +public class Sample +{ + // + public void AddSqlParameter(SqlCommand command) + { + command.Parameters.Add(new SqlParameter("Description", "Beverages")); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameterCollection_Add1.cs b/doc/samples/SqlParameterCollection_Add1.cs new file mode 100644 index 0000000000..a0843559d6 --- /dev/null +++ b/doc/samples/SqlParameterCollection_Add1.cs @@ -0,0 +1,16 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +public class Sample +{ + // + public void AddSqlParameter(SqlCommand command) + { + SqlParameter param = new SqlParameter( + "@Description", SqlDbType.NVarChar, 16); + param.Value = "Beverages"; + command.Parameters.Add(param); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameterCollection_Add3.cs b/doc/samples/SqlParameterCollection_Add3.cs new file mode 100644 index 0000000000..c77a01dcb6 --- /dev/null +++ b/doc/samples/SqlParameterCollection_Add3.cs @@ -0,0 +1,16 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +public class Sample +{ + // + public void AddSqlParameter(SqlCommand command) + { + SqlParameter param = command.Parameters.Add( + "@Description", SqlDbType.NVarChar); + param.Size = 16; + param.Value = "Beverages"; + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameterCollection_Add5.cs b/doc/samples/SqlParameterCollection_Add5.cs new file mode 100644 index 0000000000..c7c15f4a2b --- /dev/null +++ b/doc/samples/SqlParameterCollection_Add5.cs @@ -0,0 +1,13 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +public class Sample +{ +// + public void AddSqlParameter(SqlCommand cmd) + { + SqlParameter p1 = cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 16, "Description"); + } +// +} \ No newline at end of file diff --git a/doc/samples/SqlParameterCollection_Add6.cs b/doc/samples/SqlParameterCollection_Add6.cs new file mode 100644 index 0000000000..d862d0d1f9 --- /dev/null +++ b/doc/samples/SqlParameterCollection_Add6.cs @@ -0,0 +1,31 @@ +using System; +using System.Xml; +using System.Data; +using Microsoft.Data.SqlClient; +using System.Data.Common; +using System.Windows.Forms; + +public class Form1: Form +{ + protected DataSet categoriesDataSet; + protected DataGrid dataGrid1; + protected SqlDataAdapter categoriesAdapter; + + +// +public void AddSqlParameters() +{ +// ... +// create categoriesDataSet and categoriesAdapter +// ... + + categoriesAdapter.SelectCommand.Parameters.Add( + "@CategoryName", SqlDbType.VarChar, 80).Value = "toasters"; + categoriesAdapter.SelectCommand.Parameters.Add( + "@SerialNum", SqlDbType.Int).Value = 239; + categoriesAdapter.Fill(categoriesDataSet); + +} +// + +} \ No newline at end of file diff --git a/doc/samples/SqlParameterCollection_AddWithValue.cs b/doc/samples/SqlParameterCollection_AddWithValue.cs new file mode 100644 index 0000000000..c3ee6cfc0b --- /dev/null +++ b/doc/samples/SqlParameterCollection_AddWithValue.cs @@ -0,0 +1,57 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + + +class Program +{ + static void Main() + { + string connectionString = GetConnectionString(); + string demo = @"1500000150000Primary InternationalOS1974Road380003DSL40"; + Int32 id = 3; + UpdateDemographics(id, demo, connectionString); + Console.ReadLine(); + } + // + private static void UpdateDemographics(Int32 customerID, + string demoXml, string connectionString) + { + // Update the demographics for a store, which is stored + // in an xml column. + string commandText = "UPDATE Sales.Store SET Demographics = @demographics " + + "WHERE CustomerID = @ID;"; + + using (SqlConnection connection = new SqlConnection(connectionString)) + { + SqlCommand command = new SqlCommand(commandText, connection); + command.Parameters.Add("@ID", SqlDbType.Int); + command.Parameters["@ID"].Value = customerID; + + // Use AddWithValue to assign Demographics. + // SQL Server will implicitly convert strings into XML. + command.Parameters.AddWithValue("@demographics", demoXml); + + try + { + connection.Open(); + Int32 rowsAffected = command.ExecuteNonQuery(); + Console.WriteLine("RowsAffected: {0}", rowsAffected); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + } + // + static private string GetConnectionString() + { + // To avoid storing the connection string in your code, + // you can retrieve it from a configuration file. + return "Data Source=(local);Initial Catalog=AdventureWorks;" + + "Integrated Security=SSPI"; + } +} + + diff --git a/doc/samples/SqlParameterCollection_Count.cs b/doc/samples/SqlParameterCollection_Count.cs new file mode 100644 index 0000000000..a2aafd381c --- /dev/null +++ b/doc/samples/SqlParameterCollection_Count.cs @@ -0,0 +1,74 @@ +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. + string summaryString = CreateSqlParameters(7); + Console.ReadLine(); + } + // + static private string CreateSqlParameters(int documentID) + { + // Assumes GetConnectionString returns a valid connection string to the + // AdventureWorks sample database on an instance of SQL Server 2005. + using (SqlConnection connection = + new SqlConnection(GetConnectionString())) + { + connection.Open(); + SqlCommand command = connection.CreateCommand(); + try + { + // Setup the command to execute the stored procedure. + command.CommandText = "GetDocumentSummary"; + command.CommandType = CommandType.StoredProcedure; + + // Create the input parameter for the DocumentID. + SqlParameter paramID = + new SqlParameter("@DocumentID", SqlDbType.Int); + paramID.Value = documentID; + command.Parameters.Add(paramID); + + // Create the output parameter to retrieve the summary. + SqlParameter paramSummary = + new SqlParameter("@DocumentSummary", SqlDbType.NVarChar, -1); + paramSummary.Direction = ParameterDirection.Output; + command.Parameters.Add(paramSummary); + + // List the parameters and some of properties. + SqlParameterCollection paramCollection = command.Parameters; + string parameterList = ""; + for (int i = 0; i < paramCollection.Count; i++) + { + parameterList += String.Format(" {0}, {1}, {2}\n", + paramCollection[i], paramCollection[i].DbType, + paramCollection[i].Direction); + } + Console.WriteLine("Parameter Collection:\n" + parameterList); + + // Execute the stored procedure; retrieve + // and display the output parameter value. + command.ExecuteNonQuery(); + Console.WriteLine((String)(paramSummary.Value)); + return (String)(paramSummary.Value); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return null; + } + } + } + // + static private string GetConnectionString() + { + // To avoid storing the connectionection string in your code, + // you can retrieve it from a configuration file, using the + // System.Configuration.ConfigurationSettings.AppSettings property + return "Data Source=(local);Initial Catalog=AdventureWorks;" + + "Integrated Security=SSPI"; + } +} \ No newline at end of file diff --git a/doc/samples/SqlParameterCollection_Remove.cs b/doc/samples/SqlParameterCollection_Remove.cs new file mode 100644 index 0000000000..4fd2516e12 --- /dev/null +++ b/doc/samples/SqlParameterCollection_Remove.cs @@ -0,0 +1,26 @@ +using System; +using System.Xml; +using System.Data; +using Microsoft.Data.SqlClient; +using System.Data.Common; +using System.Windows.Forms; + +public class Form1: Form +{ + protected DataSet DataSet1; + protected DataGrid dataGrid1; + protected SqlCommand command; + protected SqlParameter param; + + // + public void SearchSqlParams() + { + // ... + // create SqlCommand command and SqlParameter param + // ... + if (command.Parameters.Contains(param)) + command.Parameters.Remove(param); + } + // + +} \ No newline at end of file diff --git a/doc/samples/SqlParameter_IsNullable.cs b/doc/samples/SqlParameter_IsNullable.cs new file mode 100644 index 0000000000..2f912a0c47 --- /dev/null +++ b/doc/samples/SqlParameter_IsNullable.cs @@ -0,0 +1,34 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + private static void AddSqlParameter(SqlCommand command, + string paramValue) + { + SqlParameter parameter = new SqlParameter( + "@Description", SqlDbType.VarChar); + parameter.Value = paramValue; + parameter.IsNullable = true; + command.Parameters.Add(parameter); + } + +private static void SetParameterToNull(IDataParameter parameter) +{ + if (parameter.IsNullable) + { + parameter.Value = DBNull.Value; + } + else + { + throw new ArgumentException("Parameter provided is not nullable", "parameter"); + } +} + + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameter_ParameterName.cs b/doc/samples/SqlParameter_ParameterName.cs new file mode 100644 index 0000000000..a8ee3e341b --- /dev/null +++ b/doc/samples/SqlParameter_ParameterName.cs @@ -0,0 +1,22 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + private static void AddSqlParameter(SqlCommand command) + { + SqlParameter parameter = new SqlParameter(); + parameter.ParameterName = "@Description"; + parameter.IsNullable = true; + parameter.DbType = DbType.String; + parameter.Direction = ParameterDirection.Output; + + command.Parameters.Add(parameter); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameter_Precision.cs b/doc/samples/SqlParameter_Precision.cs new file mode 100644 index 0000000000..08cf78cff6 --- /dev/null +++ b/doc/samples/SqlParameter_Precision.cs @@ -0,0 +1,21 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + private static void AddSqlParameter(SqlCommand command) + { + SqlParameter parameter = new SqlParameter("@Price", SqlDbType.Decimal); + parameter.Value = 3.1416; + parameter.Precision = 8; + parameter.Scale = 4; + + command.Parameters.Add(parameter); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameter_SqlParameter.cs b/doc/samples/SqlParameter_SqlParameter.cs new file mode 100644 index 0000000000..e5b0ddfcc7 --- /dev/null +++ b/doc/samples/SqlParameter_SqlParameter.cs @@ -0,0 +1,23 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + private static void AddSqlParameter(SqlCommand command) + { + SqlParameter parameter = new SqlParameter(); + parameter.ParameterName = "@Description"; + parameter.IsNullable = true; + parameter.SqlDbType = SqlDbType.VarChar; + parameter.Direction = ParameterDirection.Output; + parameter.Size = 88; + + command.Parameters.Add(parameter); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameter_SqlParameter1.cs b/doc/samples/SqlParameter_SqlParameter1.cs new file mode 100644 index 0000000000..b9c6b3ade2 --- /dev/null +++ b/doc/samples/SqlParameter_SqlParameter1.cs @@ -0,0 +1,22 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + private static void AddSqlParameter(SqlCommand command) + { + SqlParameter parameter = new SqlParameter("@Description", + SqlDbType.VarChar, 11, ParameterDirection.Input, + true, 0, 0, "Description", DataRowVersion.Current, + "garden hose"); + parameter.IsNullable = true; + + command.Parameters.Add(parameter); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameter_SqlParameter2.cs b/doc/samples/SqlParameter_SqlParameter2.cs new file mode 100644 index 0000000000..1ab6b4923d --- /dev/null +++ b/doc/samples/SqlParameter_SqlParameter2.cs @@ -0,0 +1,22 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + private static void AddSqlParameter(SqlCommand command, string paramValue) + { + SqlParameter parameter = new SqlParameter("@Description", SqlDbType.VarChar); + parameter.IsNullable = true; + parameter.Direction = ParameterDirection.Output; + parameter.Size = 88; + parameter.Value = paramValue; + + command.Parameters.Add(parameter); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameter_SqlParameter4.cs b/doc/samples/SqlParameter_SqlParameter4.cs new file mode 100644 index 0000000000..c1a05b3a3a --- /dev/null +++ b/doc/samples/SqlParameter_SqlParameter4.cs @@ -0,0 +1,23 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + private static void AddSqlParameter(SqlCommand command, + string paramValue) + { + SqlParameter parameter = new SqlParameter("@Description", + SqlDbType.VarChar, 88); + parameter.IsNullable = true; + parameter.Direction = ParameterDirection.Output; + parameter.Value = paramValue; + + command.Parameters.Add(parameter); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameter_SqlParameter5.cs b/doc/samples/SqlParameter_SqlParameter5.cs new file mode 100644 index 0000000000..26093fecd8 --- /dev/null +++ b/doc/samples/SqlParameter_SqlParameter5.cs @@ -0,0 +1,21 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + private static void AddSqlParameter(SqlCommand command) + { + SqlParameter parameter = new SqlParameter("@Description", + SqlDbType.VarChar, 88, "Description"); + parameter.IsNullable = true; + parameter.Direction = ParameterDirection.Output; + + command.Parameters.Add(parameter); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlParameter_SqlParameter6.cs b/doc/samples/SqlParameter_SqlParameter6.cs new file mode 100644 index 0000000000..26093fecd8 --- /dev/null +++ b/doc/samples/SqlParameter_SqlParameter6.cs @@ -0,0 +1,21 @@ +using System; +using System.Data; +using Microsoft.Data.SqlClient; + +class Program +{ + static void Main() + { + } + // + private static void AddSqlParameter(SqlCommand command) + { + SqlParameter parameter = new SqlParameter("@Description", + SqlDbType.VarChar, 88, "Description"); + parameter.IsNullable = true; + parameter.Direction = ParameterDirection.Output; + + command.Parameters.Add(parameter); + } + // +} \ No newline at end of file diff --git a/doc/samples/SqlRowUpdatingEventArgs.cs b/doc/samples/SqlRowUpdatingEventArgs.cs new file mode 100644 index 0000000000..ade2ef10a0 --- /dev/null +++ b/doc/samples/SqlRowUpdatingEventArgs.cs @@ -0,0 +1,76 @@ +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; + + + // + // 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 CONNECTION_STRING = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"; + const string SELECT_ALL = "select * from Products"; + + //Create DataAdapter. + SqlDataAdapter rAdapter = new SqlDataAdapter(SELECT_ALL, CONNECTION_STRING); + + //Create and fill DataSet (Select only first 5 rows.). + DataSet rDataSet = new DataSet(); + rAdapter.Fill(rDataSet, 0, 5, "Table"); + + //Modify DataSet. + DataTable rTable = rDataSet.Tables["Table"]; + rTable.Rows[0][1] = "new product"; + + //Add handlers. + rAdapter.RowUpdating += new SqlRowUpdatingEventHandler( OnRowUpdating ); + rAdapter.RowUpdated += new SqlRowUpdatedEventHandler( OnRowUpdated ); + + //Update--this operation fires two events (RowUpdating and RowUpdated) for each changed row. + rAdapter.Update(rDataSet, "Table"); + + //Remove handlers. + rAdapter.RowUpdating -= new SqlRowUpdatingEventHandler( OnRowUpdating ); + rAdapter.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 + ")"); + } + // + +} \ No newline at end of file