This is my class:
public class DbHelper
{
public static DataTable Select(string query, object[,] parameters)
{
DataTable dt = new DataTable();
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString());
SqlCommand com = new SqlCommand();
com.Connection = cn;
com.CommandText = query;
for (int i = 0; i < parameters.Length/2; i++)
com.Parameters.AddWithValue(parameters[i, 0].ToString(), parameters[i, 1]);
try
{
cn.Open();
dt.Load(com.ExecuteReader());
}
catch
{
}
finally
{
com.Dispose();
cn.Close();
cn.Dispose();
}
return dt;
}
}
I am using this class for select from data base, Like this:
public static string NewsListTitle(int count, int categoryId, string domainName)
{
StringBuilder cnt = new StringBuilder();
DataTable dt = DbHelper.Select("SELECT top " + count + " ..... ");
foreach (DataRow i in dt.Rows)
{
cnt.Append("<li><a target=\"_blank\" href=\"http://" + domainName + "/news/" + i["NewsID"] + "\"\">" + i["Title"] + "</a></li>");
}
return cnt.ToString();
}
the DataTable type is a powerful way to store data in memory. i thing this is best way to select from data base, but my problem is DateTable memory usage! is there any wrong here or is there any better way?
Thank you