System.Data.SqlClient.SqlCommand Extension Methods

SqlCommand.ExecuteDataSet Method

Executes the query, and returns the result set as DataSet.

Syntax

DataSet ExecuteDataSet(SqlCommand @this)

Parameters

@this
The @this to act on.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System.Data;
 using System.Data.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_SqlClient_SqlCommand_ExecuteDataSet
     {
         [TestMethod]
         public void ExecuteDataSet()
         {
             DataSet value;
             string sql = @"
 SELECT 1
 SELECT 2
 SELECT 3
 ";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
                     @this.CommandText = sql;
                     value = @this.ExecuteDataSet(); // return DataSet (3 tables).
                 }
             }
 
             // Unit Test
             Assert.AreEqual(3, value.Tables.Count);
         }
     }
 }
                

SqlCommand.ExecuteDataTable Method

Executes the query, and returns the first result set as DataTable.

Syntax

DataTable ExecuteDataTable(SqlCommand @this)

Parameters

@this
The @this to act on.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System.Data;
 using System.Data.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_SqlClient_SqlCommand_ExecuteDataTable
     {
         [TestMethod]
         public void ExecuteDataTable()
         {
             DataTable value;
             string sql = @"
 SELECT  1 AS A
 UNION
 SELECT  2
 UNION
 SELECT  3
 ";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
                     @this.CommandText = sql;
                     value = @this.ExecuteDataTable(); // return DataTable (3 rows).
                 }
             }
 
             // Unit Test
             Assert.AreEqual(3, value.Rows.Count);
         }
     }
 }
                

Last edited Nov 23, 2013 at 6:53 PM by Jonathan_Magnan, version 1