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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:24:37+00:00 2026-06-16T22:24:37+00:00

Trying to improve my C# to SQL skills… Currently I am using this bit

  • 0

Trying to improve my C# to SQL skills… Currently I am using this bit of code to pull data from our application server. I have two different DBA’s telling me two other ways to write this, just trying to figure out if this should be improved on or changed. If so, I would really appreciate some kind of examples.

FYI: This code…

db.con(user.Authority)

…Is essentially a ‘new sqlconnection’ code.

DataTable dtInfo = new DataTable("SomeInfo");
using (SqlConnection con = db.con(user.Authority))
        {
            string command = "SOME SQL STATEMENT;";
            using (SqlCommand cmd = new SqlCommand(command,con))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Param", sqlDbType).Value = Param;
                con.Open();
                cmd.ExecuteNonQuery();

                **********
                *** OR ***
                **********

                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Param", sqlDbType).Value = Param;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(dtInfo );
                }
            }
        }

So, if I’m understanding the provided information, this is my best route?

        using (SqlConnection con = db.con(user.Authority))
        {
            string command = "SELECT [TBL_EMPLOYEE].[ACTIVE_DIRECTORY] FROM [TBL_EMPLOYEE];";
            using (SqlCommand cmd = new SqlCommand(command, con))
            {
                con.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        MessageBox.Show(reader["ACTIVE_DIRECTORY"].ToString());
                    }
                }
            }

And one last thing… This should prevent the need for

cmd.Dispose();
etc...
  • 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-16T22:24:39+00:00Added an answer on June 16, 2026 at 10:24 pm

    I would not use the SqlDataAdapter version. The version that uses the SqlCommand object and the SqlDataReader will perform better, and allows more insight into the actual data being returned.

    // Assumes the following sql:
    //   SELECT foo, bar FROM baz
    
    // error checking left out for simplicity
    var list = new List<SomeClass>();
    using(var reader = cmd.ExecuteReader()) {
       while(reader.Read()) {
          list.Add(new SomeClass {
             // NOTE: you can see the columns that the c# is referencing
             //       and compare them to the sql statement being executed
             Foo = (string)reader["foo"],
             Bar = (string)reader["bar"]
          });
       }
    }
    

    Later as your level of experiance increases you will be able to use other features of the SqlCommand and SqlDataReader classes in order to ensure that the code executes as quickly as possible. If you start using the SqlDataAdapter route, you will eventually have to relearn how to do the exact same things you have already been doing because the SqlCommand and SqlDataReader have operations that do not exist elsewhere in .NET.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to improve my application's security. Whenever I receive data from the
I am trying to improve my jQuery skill and I have this bit of
Ok.. So I'm trying to improve my SQL skills and have a question. Here
I'm trying to not write an app to do this, and improve my SQL
I am trying to improve query readability by using a function in SQL Server
I am trying to do something similar to How can I improve this SQL
I am trying to improve performance on an application using Hibernate which is executing
I'm trying to improve a function which selects data from hsqldb so I tried
I'm trying to improve the performance of this code: orderitems = OrderItem.objects.filter(order__contact=contact) for orderitem
I'm trying to improve FxCop compliance in my code (for the first time ever),

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.