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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:51:01+00:00 2026-05-27T16:51:01+00:00

The reason I ask, is that origionally all references and examples pointed towards Method

  • 0

The reason I ask, is that origionally all references and examples pointed towards Method 2.

But Method 1 seems to do so much more in so much less code. Surely its

  1. neater,
  2. Better
  3. Easier to read / follow
  4. and has features like converting DBNulls to null. Rather than having
    to do it myself.
  5. Handles opening and closing of the connection/ data readers.

I cant think of any reasons for method 2 over using method 1.

(Method 2 could probably have a using clause, but due to so much code, it was put in a seperate function to be called from else wher, and hence closing it closed the datareader making data inacccessable.)

Anyway, I am seriously thinking about going back and changing over my entire webservice to use method 1 as it seems so much better and maintainable. Can anyone suggest otherwise?

Method 1:

using (var db = Database.OpenConnectionString(Library.Properties.Settings.Default.dbConnString, "System.Data.SqlClient"))
        {
            Int32 AccNo = db.QuerySingle("SELECT AccNo FROM Tasks WHERE TaskID = " + TaskID);
        }

Method 2:

        sqlComm = new SqlCommand();
        sqlCon = new SqlConnection();
        sqlComm.Connection = sqlCon;

        sqlCon.ConnectionString = global::Library.Properties.Settings.Default.dbConnString;
        sqlComm.CommandText = "SELECT AccNo FROM Tasks WHERE TaskID = " + TaskID;
        sqlCon.Open();
        SqlDataReader data = sqlComm.ExecuteReader();
        while (data.Read())
        {
            Int32 AccNo = (Int32)data["AccNo"];
        }
        sqlCon.Close();
        sqlComm.Dispose();
        if (data != null)
           data.Close();
  • 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-27T16:51:02+00:00Added an answer on May 27, 2026 at 4:51 pm

    Try using the entity framework

    using (var context = new YourDatabaseEntities())
    {
        var elements = (from c in context.YourTable where c.TaskId == taskId select c);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The reason I ask is that Stack Overflow has been Slashdotted , and Redditted
Does flash.security.allowDomain(*) allow for redirects? The reason why I ask is that I'm trying
I'd like to ask you if there is a reason to capitalize all items
The reason I ask is that we'd like to use a certain CHECK constraint
The reason I ask is that I am trying to run MVC side-by-side with
The reason I ask is that I have an application that (among other things)
Like the title says. Reason I ask is that we're converting PDFs to formatted
The reason I ask is that you can cast a nullable type to a
It works, no crashes. Is it OK? edit: the reason I ask is that
...the reason I ask is that Safari has a bug in its implementation of

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.