Click here to Skip to main content
11,641,708 members (54,603 online)
Click here to Skip to main content

An Easy Way To Query a Database

, 14 Feb 2011 CPOL 4.8K 3
Rate this:
Please Sign up or sign in to vote.
The following samples should take advantage of using statements and furthermore built in features of the ADO.NET framework. Furthermore, if you open a connection, make sure you open it at the latest time possible and close it as soon as possible.Edit: While I was originally not trying to...
The following samples should take advantage of using statements and furthermore built in features of the ADO.NET framework. Furthermore, if you open a connection, make sure you open it at the latest time possible and close it as soon as possible.

Edit: While I was originally not trying to change too much of the original content, I might as well to incorporate all of the suggestions posted below. As woric and I noticed, the exception handling is generally not a good idea, so it can be removed and handled at a higher level. Though, the using statement should be used to close the connection, so the try/finally isn't necessary. Then, as Fzelle pointed out, the SqlDataAdapter can be passed the connection string and query to simplify that method.

public DataSet select_query(string query, string con_str)
{
  using (SqlDataAdapter da = new SqlDataAdapter(query, con_str))
  {
    DataSet ds = new DataSet();
    da.Fill(ds);  // Automatically opens/closes connection.
    return ds;
  }
}
public int modify_query(string query, global_data data)
{
  using (SqlConnection con = new SqlConnection(data.Con_Str))
  {
    using (SqlCommand comd = new SqlCommand(query, con))
    {
      con.Open();
      int x = comd.ExecuteNonQuery();
      con.Close();
      return x;
    }
  }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andrew Rissing
Software Developer (Senior)
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 5 Clear and Short. Pin
gopalomar30-Mar-11 23:21
membergopalomar30-Mar-11 23:21 
GeneralReason for my vote of 5 Fine code Pin
thatraja5-Mar-11 3:52
mvpthatraja5-Mar-11 3:52 
GeneralReason for my vote of 5 Nice code, very clear and readable. Pin
Francisco Tirado22-Feb-11 3:18
memberFrancisco Tirado22-Feb-11 3:18 
GeneralHi Andrew, I would probably agree. The general idea I put in... Pin
DrABELL16-Feb-11 9:03
memberDrABELL16-Feb-11 9:03 
Hi Andrew,
I would probably agree. The general idea I put in my comment is to try avoiding any potential exception raising, because exception handling is relatively "expensive" procedure. Also, Connection object in addition to Open/Closed state has another state (StateConnecting); not sure how system will react on any attempt to perform operations or to close the connection in that state, though in synchronous mode this probably will never happen.
Best regards, Alex
GeneralI already voted 5 for this solution and would like just to p... Pin
DrABELL15-Feb-11 16:26
memberDrABELL15-Feb-11 16:26 
GeneralRe: Checking the state is unnecessary. The calling "Close" twic... Pin
Andrew Rissing16-Feb-11 3:14
memberAndrew Rissing16-Feb-11 3:14 
GeneralReason for my vote of 5 Very elegant and practical solution;... Pin
DrABELL15-Feb-11 16:20
memberDrABELL15-Feb-11 16:20 
Generalsorry dear, i had a confusion, but its clear now, thnx for s... Pin
Abdur Rehman Raza Khan - dontumindit15-Feb-11 7:55
memberAbdur Rehman Raza Khan - dontumindit15-Feb-11 7:55 
Generalwith the DataAdapter. Why do you think there is an overload ... Pin
Abdur Rehman Raza Khan - dontumindit14-Feb-11 18:36
memberAbdur Rehman Raza Khan - dontumindit14-Feb-11 18:36 
GeneralRe: I don't follow what you're trying to say. Can you complete ... Pin
Andrew Rissing15-Feb-11 4:30
memberAndrew Rissing15-Feb-11 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 14 Feb 2011
Article Copyright 2011 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid