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 3443272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:46:15+00:00 2026-05-18T08:46:15+00:00

I haven’t accessed data using SqlCommand etc. for a while as I tend to

  • 0

I haven’t accessed data using SqlCommand etc. for a while as I tend to use NHibernate these days. I am just wondering whether the following code could be improved. I have tried to use best practises (after some google-ing) and potential exceptions are caught at a higher layer.

[WebMethod]
    public XmlDocument GetClassRegistrationReport()
    {
        XmlDocument doc = new XmlDocument();

        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["bla"].ToString()))
        {
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = "bla";
                command.CommandType = CommandType.StoredProcedure;
                connection.Open();
                doc.Load(command.ExecuteXmlReader());
            }
        }

        return doc;
    }

Thanks!

Best wishes,

Christian

  • 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-05-18T08:46:16+00:00Added an answer on May 18, 2026 at 8:46 am

    There are a few ways you could improve it a little:

    • Although the WebMethod pulls data and returns it verbatim with no input parameters, I would suggest seperating service interface and the data into seperate classes. It may make things easier to maintain at a later date.
    • Assuming there are other DB calls in your framework you may want to consider a helper method in your data layer that wraps up the invocation of a stored procedure. This way you only have one method that all SP calls filter down into which again will make things easier to maintain in the future.
    • Make the ‘bla’ key for your connection string setting a constant, this way you can easily reuse and change.
    • The same applies to the name of the stored procedure, alternatively make it part of your web.config – this means you can change the stored proc name without having to recompile.
    • If an exception is throw there is no handling for this so the exception will bubble out to the caller, consider catching and handling/logging exceptions. That said you do mention that you are handling exceptions at a higher layer, so I assume this is being done in whatever is calling your webservices.
    • You should be disposing the SQL command object (in the finally of the try/catch/finally if you do implement exception handling)

    EDIT : Code Sample

    public class MyWebService
    {
        [WebMethod]
        public XmlDocument GetClassRegistrationReport()
        {
            return DataLayer.GetClassRegistrationReport();
        }
    }
    // Notice that this is a static internal class, internal to hide the
    // data access class from everything but this library and static because
    // we don't need instances and using statics will optimise a little.
    internal static class DataLayer
    {
        private const string SP_GetRegistrationReport = "GetRegistrationReport";
        private const string Config_DBConnectionString = "PrimaryDB";
    
        private static string GetDB
        {
            get
            {
                string dbConnectionString = ConfigurationManager.ConnectionStrings[Config_DBConnectionString].ConnectionString;
    
                if (string.IsNullOrEmpty(dbConnectionString))
                {
                    // This error should could/should be in a resource file.
                    throw new ConfigurationException("Database connection string is not defined");
                }
    
                return dbConnectionString;
            }
        }
    
        internal static XmlDocument GetClassRegistrationReport()
        {
            XmlDocument doc = new XmlDocument();
    
            using (SqlConnection connection = new SqlConnection())
            {
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = SP_GetRegistrationReport;
                    command.CommandType = CommandType.StoredProcedure;
                    connection.Open();
                    doc.Load(command.ExecuteXmlReader());
                }
            }
    
            return doc;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.