diff --git a/doc/samples/SqlDataRecord.cs b/doc/samples/SqlDataRecord.cs index f81de5b850..e060723ef7 100644 --- a/doc/samples/SqlDataRecord.cs +++ b/doc/samples/SqlDataRecord.cs @@ -2,10 +2,9 @@ namespace SqlDataRecordCS; using System; +using System.Collections.Generic; using System.Data; -using System.Data.Sql; -using System.Data.SqlTypes; -using Microsoft.SqlServer.Server; +using Microsoft.Data.SqlClient.Server; public sealed partial class SqlDataRecordTester { @@ -13,41 +12,40 @@ private SqlDataRecordTester() { } -[SqlProcedure] -public static void CallTestMethods() -{ - CreateNewRecord(); - CreateNewRecord1(); - -} - // +//using System; +//using System.Collections.Generic; +//using System.Data; //using Microsoft.Data.SqlClient.Server; -[SqlProcedure] -public static void CreateNewRecord() +// Stream rows to SQL Server as a table-valued parameter. +public static IEnumerable GetRecords() { - - // Variables. - SqlDataRecord record; + // Re-use a single SqlDataRecord instance rather than allocating a new one for each row. + // Each row's values are read before SqlCommand advances to the next one. + SqlDataRecord record; - // Create a new record with the column metadata. The constructor is - // able to accept a variable number of parameters. - record = new SqlDataRecord(new SqlMetaData[] { new SqlMetaData("Column1", SqlDbType.NVarChar, 12), + // Create a new record with the column metadata. The constructor is + // able to accept a variable number of parameters. + record = new SqlDataRecord(new SqlMetaData[] { new SqlMetaData("Column1", SqlDbType.NVarChar, 12), new SqlMetaData("Column2", SqlDbType.Int), new SqlMetaData("Column3", SqlDbType.DateTime) }); - // Set the record fields. - record.SetString(0, "Hello World!"); - record.SetInt32(1, 42); - record.SetDateTime(2, DateTime.Now); + // Set the record fields. + record.SetString(0, "Hello World!"); + record.SetInt32(1, 42); + record.SetDateTime(2, DateTime.Now); - // Send the record to the calling program. - SqlContext.Pipe.Send(record); + // Set the fields of the first record and stream it to SQL Server. + yield return record; + + // Set the fields of the second record and stream it to SQL Server. + record.SetInt32(1, 0); + yield return record; } // -public static void CreateNewRecord1() +public static void CreateNewRecord() { // @@ -65,15 +63,11 @@ public static void CreateNewRecord1() // Create a new record with the column metadata. record = new SqlDataRecord(new SqlMetaData[] { column1Info, column2Info }); -// - // Set the record fields. record.SetString(0, "Hello World!"); record.SetInt32(1, 42); -// Send the record to the calling program. -SqlContext.Pipe.Send(record); - +// } } #endif diff --git a/doc/samples/SqlMetaData.cs b/doc/samples/SqlMetaData.cs index c23cf363b7..fd6e2fff6b 100644 --- a/doc/samples/SqlMetaData.cs +++ b/doc/samples/SqlMetaData.cs @@ -2,10 +2,9 @@ namespace SqlMetaDataCS; using System; +using System.Collections.Generic; using System.Data; -using System.Data.Sql; -using System.Data.SqlTypes; -using Microsoft.SqlServer.Server; +using Microsoft.Data.SqlClient.Server; public sealed partial class SqlMetaDataTester { @@ -14,10 +13,12 @@ private SqlMetaDataTester() } // + // using System; + // using System.Collections.Generic; + // using System.Data; // using Microsoft.Data.SqlClient.Server; - [SqlProcedure] - public static void CreateNewRecord() + public static IEnumerable ReturnNewRecords() { // Variables. SqlMetaData column1Info; @@ -35,13 +36,15 @@ public static void CreateNewRecord() column2Info, column3Info }); - // Set the record fields. + // Set the fields of the first record and stream it to SQL Server. record.SetString(0, "Hello World!"); record.SetInt32(1, 42); record.SetDateTime(2, DateTime.Now); + yield return record; - // Send the record to the calling program. - SqlContext.Pipe.Send(record); + // Set the fields of the second record and stream it to SQL Server. + record.SetInt32(1, 0); + yield return record; } // diff --git a/doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml b/doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml index 7b0af6f6c9..6ec13713d2 100644 --- a/doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml +++ b/doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml @@ -1,4 +1,4 @@ - + @@ -6,25 +6,28 @@ - This class is used together with to send result sets to the client from managed code stored-procedures. + This class describes a single row of a table-valued parameter. Construct one from an array of objects which describe the column metadata of the record, populate it with the Set<Type> methods and assign it (or an for multiple rows) to . - When writing common language runtime (CLR) applications, you should re-use existing SqlDataRecord objects instead of creating new ones every time. Creating many new SqlDataRecord objects could severely deplete memory and adversely affect performance. + For best performance when sending multiple rows, re-use a single instance across an iterator: set new column values and the same instance for each row. will read each record before advancing to the next. - The following example shows how to create several objects, which describe the column metadata of a record, and creating a . The column values of the are set and the is sent to the calling program by using the class. + The following example shows how to create several objects describing the column metadata of a record, then to create a re-usable instance. The column values of the are set and the same can be used to stream multiple rows to SQL Server as a table-valued parameter. + using System; + using System.Collections.Generic; + using System.Data; using Microsoft.Data.SqlClient.Server; - - [Microsoft.Data.SqlClient.Server.SqlProcedure] - public static void CreateNewRecord() + + // Stream rows to SQL Server as a table-valued parameter. + public static IEnumerable<SqlDataRecord> CreateNewRecord() { - - // Variables. + // Re-use a single SqlDataRecord instance rather than allocating a new one for each row. + // Each row's values are read before SqlCommand advances to the next one. SqlDataRecord record; // Create a new record with the column metadata. The constructor is @@ -38,8 +41,12 @@ record.SetInt32(1, 42); record.SetDateTime(2, DateTime.Now); - // Send the record to the calling program. - SqlContext.Pipe.Send(record); + // Set the fields of the first record and stream it to SQL Server. + yield return record; + + // Set the fields of the second record and stream it to SQL Server. + record.SetInt32(1, 0); + yield return record; } @@ -75,9 +82,6 @@ // Set the record fields. record.SetString(0, "Hello World!"); record.SetInt32(1, 42); - - // Send the record to the calling program. - SqlContext.Pipe.Send(record); diff --git a/doc/snippets/Microsoft.Data.SqlClient.Server/SqlMetaData.xml b/doc/snippets/Microsoft.Data.SqlClient.Server/SqlMetaData.xml index cbabfd8cb0..2b4c009c7f 100644 --- a/doc/snippets/Microsoft.Data.SqlClient.Server/SqlMetaData.xml +++ b/doc/snippets/Microsoft.Data.SqlClient.Server/SqlMetaData.xml @@ -1,19 +1,21 @@ - + - Specifies and retrieves metadata information from parameters and columns of objects. This class cannot be inherited. + Specifies the metadata information of a columns used by a objects. This class cannot be inherited. - The following example shows the creation of several objects, which describe the column metadata of a record, and the creation of a . The column values of the are set and the is sent to the calling program using the class. + The following example shows the creation of several objects, which describe the column metadata of a record, and the generation of a stream of records. These records can be streamed to SQL Server as a table-valued parameter by assigning the return value of the method to the property. + using System; + using System.Collections.Generic; + using System.Data; using Microsoft.Data.SqlClient.Server; - - [Microsoft.Data.SqlClient.Server.SqlProcedure] - public static void CreateNewRecord() + + public static IEnumerable<SqlDataRecord> ReturnNewRecords() { // Variables. SqlMetaData column1Info; @@ -32,16 +34,21 @@ column2Info, column3Info }); - // Set the record fields. + // Set the fields of the first record and stream it to SQL Server. record.SetString(0, "Hello World!"); record.SetInt32(1, 42); record.SetDateTime(2, DateTime.Now); + yield return record; - // Send the record to the calling program. - SqlContext.Pipe.Send(record); + // Set the fields of the second record and stream it to SQL Server. + record.SetInt32(1, 0); + yield return record; } + + instances are typically built once and reused to construct many objects which are streamed to SQL Server as a table-valued parameter. For more information, see Table-Valued Parameters. + @@ -456,7 +463,7 @@ The SQL Server type name for . - Initializes a new instance of the class with the specified column name, user-defined type (UDT), and SQLServer type. + Initializes a new instance of the class with the specified column name, user-defined type (UDT), and SQL Server type.