I am writing a method to return the string "asset" from the database. It contains strings, ints and an array of bytes (this can be an image / movie / document).
Now, to access most strings, I use the following method, which returns a NameValueCollection, since it is a light weighted object, easy to use, and distinguishes between int and strings.
public static NameValueCollection ReturnNameValueCollection(Database db, DbCommand dbCommand)
{
var nvc = new NameValueCollection();
using (IDataReader dr = db.ExecuteReader(dbCommand))
{
if (dr != null)
{
while (dr.Read())
{
for (int count = 0; count < dr.FieldCount; count++)
{
nvc[dr.GetName(count)] = dr.GetValue(count).ToString();
}
}
}
}
dbCommand.Dispose();
return nvc.Count != 0 ? nvc : null;
}
Now my apporach for such data access, as a rule, should get a method to return datarow.
public static DataRow ReturnDataRow(Database db, DbCommand dbCommand)
{
var dt = new DataTable();
using (IDataReader dr = db.ExecuteReader(dbCommand))
if (dr != null) dt.Load(dr);
dbCommand.Dispose();
return dt.Rows.Count != 0 ? dt.Rows[0] : null;
}
It seems like it's useless to create a DataTable and then return your first datarow.
Is there a better way to do this?
I think maybe a dictionary of objects, which I then manually dropped every member.
, . , - , DataSets (, , ), .
, , , .