System.Data.Common.DbCommand Extension Methods

DbCommand.ExecuteBoolean Method

Executes the query, and returns the first column of the first row in the result set returned by the query as
a Boolean. Additional columns or rows are ignored.

Syntax

bool ExecuteBoolean(DbCommand @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.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_Common_DbCommand_ExecuteBoolean
     {
         [TestMethod]
         public void ExecuteBoolean()
         {
             bool value;
             string sql = @"SELECT 1";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
                     @this.CommandText = sql;
                     value = @this.ExecuteBoolean(); // return true;
                 }
             }
 
             // Unit Test
             Assert.IsTrue(value);
         }
     }
 }
                

DbCommand.ExecuteDateTime Method

Executes the query, and returns the first column of the first row in the result set returned by the query as
a DateTime. Additional columns or rows are ignored.

Syntax

DateTime ExecuteDateTime(DbCommand @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;
 using System.Data.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_Common_DbCommand_ExecuteDateTime
     {
         [TestMethod]
         public void ExecuteDateTime()
         {
             DateTime value;
             string sql = @"SELECT '2013/04/13'";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
                     @this.CommandText = sql;
                     value = @this.ExecuteDateTime(); // return "2013/04/13"
                 }
             }
 
             // Unit Test
             Assert.AreEqual(0, new DateTime(2013, 04, 13).Subtract(value).TotalMilliseconds);
         }
     }
 }
                

DbCommand.ExecuteDouble Method

Executes the query, and returns the first column of the first row in the result set returned by the query as
a Double. Additional columns or rows are ignored.

Syntax

double ExecuteDouble(DbCommand @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.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_Common_DbCommand_ExecuteDouble
     {
         [TestMethod]
         public void ExecuteDouble()
         {
             double value;
             string sql = @"SELECT 123.456";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
                     @this.CommandText = sql;
                     value = @this.ExecuteDouble(); // return 123.456;
                 }
             }
 
             // Unit Test
             Assert.AreEqual(123.456, value);
         }
     }
 }
                

DbCommand.ExecuteInt Method

Executes the query, and returns the first column of the first row in the result set returned by the query as
an Int. Additional columns or rows are ignored.

Syntax

int ExecuteInt(DbCommand @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.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_Common_DbCommand_ExecuteInt
     {
         [TestMethod]
         public void ExecuteInt()
         {
             int value;
             string sql = @"SELECT 123";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
                     @this.CommandText = sql;
                     value = @this.ExecuteInt(); // return 123;
                 }
             }
 
             // Unit Test
             Assert.AreEqual(123, value);
         }
     }
 }
                

DbCommand.ExecuteNullableBoolean Method

Executes the query, and returns the first column of the first row in the result set returned by the query as
a nullable Boolean. Additional columns or rows are ignored.

Syntax

bool? ExecuteNullableBoolean(DbCommand @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.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_Common_DbCommand_ExecuteNullableBoolean
     {
         [TestMethod]
         public void ExecuteNullableBoolean()
         {
             bool? value1;
             bool? value2;
 
             string sql1 = @"SELECT 123";
             string sql2 = @"SELECT NULL";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
 
                     @this.CommandText = sql1;
                     value1 = @this.ExecuteNullableBoolean(); // return 123;
 
                     @this.CommandText = sql2;
                     value2 = @this.ExecuteNullableBoolean(); // return NULL;
                 }
             }
 
             // Unit Test
             Assert.IsTrue(value1.HasValue);
             Assert.IsFalse(value2.HasValue);
         }
     }
 }
                

DbCommand.ExecuteNullableDateTime Method

Executes the query, and returns the first column of the first row in the result set returned by the query as
a nullable DateTime. Additional columns or rows are ignored.

Syntax

DateTime? ExecuteNullableDateTime(DbCommand @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;
 using System.Data.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_Common_DbCommand_ExecuteNullableDateTime
     {
         [TestMethod]
         public void ExecuteNullableDateTime()
         {
             DateTime? value1;
             DateTime? value2;
 
             string sql1 = @"SELECT '2013/04/13'";
             string sql2 = @"SELECT NULL";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
 
                     @this.CommandText = sql1;
                     value1 = @this.ExecuteNullableDateTime(); // return "2013/04/13"
 
                     @this.CommandText = sql2;
                     value2 = @this.ExecuteNullableDateTime(); // return NULL;
                 }
             }
 
             // Unit Test
             Assert.IsTrue(value1.HasValue);
             Assert.IsFalse(value2.HasValue);
         }
     }
 }
                

DbCommand.ExecuteNullableDouble Method

Executes the query, and returns the first column of the first row in the result set returned by the query as
a nullable Double. Additional columns or rows are ignored.

Syntax

double? ExecuteNullableDouble(DbCommand @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.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_Common_DbCommand_ExecuteNullableDouble
     {
         [TestMethod]
         public void ExecuteNullableDouble()
         {
             double? value1;
             double? value2;
 
             string sql1 = @"SELECT 123.456";
             string sql2 = @"SELECT NULL";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
 
                     @this.CommandText = sql1;
                     value1 = @this.ExecuteNullableDouble(); // return 123.456;
 
                     @this.CommandText = sql2;
                     value2 = @this.ExecuteNullableDouble(); // return NULL;
                 }
             }
 
             // Unit Test
             Assert.IsTrue(value1.HasValue);
             Assert.IsFalse(value2.HasValue);
         }
     }
 }
                

DbCommand.ExecuteNullableInt Method

Executes the query, and returns the first column of the first row in the result set returned by the query as
an nullable Int. Additional columns or rows are ignored.

Syntax

int? ExecuteNullableInt(DbCommand @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.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_Common_DbCommand_ExecuteNullableInt
     {
         [TestMethod]
         public void ExecuteNullableInt()
         {
             int? value1;
             int? value2;
 
             string sql1 = @"SELECT 123";
             string sql2 = @"SELECT NULL";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
 
                     @this.CommandText = sql1;
                     value1 = @this.ExecuteNullableInt(); // return 123;
 
                     @this.CommandText = sql2;
                     value2 = @this.ExecuteNullableInt(); // return NULL;
                 }
             }
 
             // Unit Test
             Assert.IsTrue(value1.HasValue);
             Assert.IsFalse(value2.HasValue);
         }
     }
 }
                

DbCommand.ExecuteString Method

Executes the query, and returns the first column of the first row in the result set returned by the query as
a String. Additional columns or rows are ignored.

Syntax

string ExecuteString(DbCommand @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.SqlClient;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Data_Common_DbCommand_ExecuteString
     {
         [TestMethod]
         public void ExecuteString()
         {
             string value;
             string sql = @"SELECT 'Fizz'";
 
             // Examples
             using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
             {
                 using (SqlCommand @this = conn.CreateCommand())
                 {
                     conn.Open();
                     @this.CommandText = sql;
                     value = @this.ExecuteString(); // return "Fizz";
                 }
             }
 
             // Unit Test
             Assert.AreEqual("Fizz", value);
         }
     }
 }
                

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