Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following example illustrates using either a <code>DataTable</code> or an <code>IEnumerable&lt;SqlDataRecord&gt;</code>:</p> <p>SQL Code</p> <pre><code>CREATE TABLE dbo.PageView ( PageViewID BIGINT NOT NULL CONSTRAINT pkPageView PRIMARY KEY CLUSTERED, PageViewCount BIGINT NOT NULL ); CREATE TYPE dbo.PageViewTableType AS TABLE ( PageViewID BIGINT NOT NULL ); CREATE PROCEDURE dbo.procMergePageView @Display dbo.PageViewTableType READONLY AS BEGIN MERGE INTO dbo.PageView AS T USING @Display AS S ON T.PageViewID = S.PageViewID WHEN MATCHED THEN UPDATE SET T.PageViewCount = T.PageViewCount + 1 WHEN NOT MATCHED THEN INSERT VALUES(S.PageViewID, 1); END </code></pre> <p>C# Code</p> <pre><code>private static void ExecuteProcedure(bool useDataTable, string connectionString, IEnumerable&lt;long&gt; ids) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "dbo.procMergePageView"; command.CommandType = CommandType.StoredProcedure; SqlParameter parameter; if (useDataTable) { parameter = command.Parameters.AddWithValue("@Display", CreateDataTable(ids)); } else { parameter = command.Parameters.AddWithValue("@Display", CreateSqlDataRecords(ids)); } parameter.SqlDbType = SqlDbType.Structured; parameter.TypeName = "dbo.PageViewTableType"; command.ExecuteNonQuery(); } } } private static DataTable CreateDataTable(IEnumerable&lt;long&gt; ids) { DataTable table = new DataTable(); table.Columns.Add("ID", typeof(long)); foreach (long id in ids) { table.Rows.Add(id); } return table; } private static IEnumerable&lt;SqlDataRecord&gt; CreateSqlDataRecords(IEnumerable&lt;long&gt; ids) { SqlMetaData[] metaData = new SqlMetaData[1]; metaData[0] = new SqlMetaData("ID", SqlDbType.BigInt); SqlDataRecord record = new SqlDataRecord(metaData); foreach (long id in ids) { record.SetInt64(0, id); yield return record; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload