65.9K
CodeProject is changing. Read more.
Home

An Easy Way To Query a Database

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Feb 14, 2011

CPOL
viewsIcon

4810

You are both wrong with the DataAdapter.Why do you think there is an overload with the querystring and the connectionstring?public DataSet select_query(string query, string con_str){ try { using (SqlDataAdapter da = new SqlDataAdapter(query,con_str)) { ...

You are both wrong with the DataAdapter. Why do you think there is an overload with the querystring and the connectionstring?
public DataSet select_query(string query, string con_str)
{
  try
  {
         using (SqlDataAdapter da = new SqlDataAdapter(query,con_str))
         {
           DataSet ds = new DataSet();
           da.Fill(ds);  // Automatically opens/closes connection.
           return ds;
         }
       }
       catch (Exception ex)
       {
            MessageBox.Show(ex.Message);  // Bad mojo, but better than nothing I guess.
            return null;
       }
}