Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:49:30+00:00 2026-06-04T17:49:30+00:00

I have a Sql Utility class which contains a lot of handy methods around

  • 0

I have a Sql Utility class which contains a lot of handy methods around Sql queries.
This class contains the following method:

public static T ExecuteScalar<T>(
    string query, 
    SqlConnection connection, 
    params SqlParameter[] parameters) 
    where T : class, new()
{
    SqlCommand command =
        CreateCommand(query, connection, parameters);

    return command.ExecuteScalar() as T;
}

Is it possible to return for example Guid objects or other non-nullable classes.

Something like this:

Guid result = 
    SqlUtils.ExecuteScalar<Guid>(
        @"SELECT [Id] FROM [dbo].[MyTable]
            WHERE [Column1] = @Param1", 
        connection, 
        new SqlParameter("@Param1", "someValue"));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:49:33+00:00Added an answer on June 4, 2026 at 5:49 pm

    You can use default(T) (and you should remove the generic type constraints):

    SqlCommand command = 
        CreateCommand(query, connection, parameters);
    
    object value = command.ExecuteScalar();
    
    if (value == null || value is DbNull)
    {
        return default(T)'
    }
    
    return (T)value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of methods that do some utility work over SQL connection,
I have a requirment to build a small utility, which fires a sql query
I have a script which im running through sql command utility and in one
I have a utility which: grabs sql commands from a flat text file executes
I have SQL query which joins 3 tables, one being just a many-to-many connecting
I have SQL like this (where $ytoday is 5 days ago): $sql = 'SELECT
Machine does not have full sql server installed. but following distributable packages are installed
I have recently discovered the tablediff utility of SQL Server 2005. I have 2
I have redesigned my Firebird based SQL database. As part of this process I
I have a shared library which containts implementation(language C) of some utility db2 procedures

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.