I am quite new to C# and to the ASP.NET programming. When maintaining the legacy code, I have found rather terrible implementation of the method that is used on many places to get the DataTable object with the data filled by the SQL command.
Here is my first approach to rewrite it. Does using work with the return as shown below? Should the da and ds be released explicitly somehow? If ds is released, is the returned DataTable reference counted so that it will not be deleted?
public DataTable getdata2(string connection_string_id, string sqlcmd)
{
string connstring = ConfigurationManager
.ConnectionStrings[connection_string_id].ConnectionString;
// Open the connection and return the first table.
// A single one should be there, only.
using (SqlConnection con = new SqlConnection(connstring))
{
SqlDataAdapter da = new SqlDataAdapter(sqlcmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
Debug.Assert(ds.Tables.Count == 1);
return ds.Tables[0];
}
}