c# - Listing the contents of a database file which is to be displayed in a View -
i'm using open source fingerprint recognition program written in c# (sourceafis) stores enrolled users in .dat file.
to read .dat file use:
list<myperson> database = new list<myperson>(); binaryformatter formatter = new binaryformatter(); if (file.exists(database)) { using (filestream stream = system.io.file.openread(database)) database = (list<myperson>)formatter.deserialize(stream); }
where database
path of database.dat file.
with can use database.add(enroll(image, name));
enroll people system.
the enroll function follows:
static myperson enroll(string filename, string name) { myfingerprint fp = new myfingerprint(); fp.filename = filename; bitmapimage image = new bitmapimage(new uri(filename, urikind.relativeorabsolute)); fp.asbitmapsource = image; myperson person = new myperson(); person.name = name; person.fingerprints.add(fp); person.fingerprints[0].finger = finger.leftthumb; afis.extract(person); return person; }
i wondering if possible list each user in database , have them displayed in 1 of asp.net mvc views? how able check way in database written to?
edit:
myperson , myfingerprint classes declared in controller
[serializable] class myperson : person { public string name; } [serializable] class myfingerprint : fingerprint { public string filename; }
the database
object in example isn't database in traditional sense. rather, it's list
of myperson
objects serialized file, including fingerprint templates , other values myperson
class contains. there document describes format used writing file, since file represents object of type list<myperson>
, there's not lot gain trying read binary format of file. i've used serialization numerous times , have never bothered @ file format. it's safer let binaryformatter
class handle work.
if haven't already, should @ documentation serialization on msdn. if myperson
class has chance of being changed in future, should @ section on version tolerant serialization avoid potential problems down road.
the database
object can passed directly view shown in following sample code. need update namespace in view reflect project. code in index
method demonstrate concept: it's not reflective of app architecture.
controller
public class mypersoncontroller : controller { public actionresult index() { list<myperson> database = new list<myperson>(); binaryformatter formatter = new binaryformatter(); if (file.exists(database)) { using (filestream stream = system.io.file.openread(database)) database = (list<myperson>)formatter.deserialize(stream); } return view(database); } }
view, listing name
property
@model ienumerable<myperson> @{ viewbag.title = "index"; } <h2>index</h2> <table> <tr> <th> @html.displaynamefor(model => model.name) </th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.name) </td> </tr> } </table>
Comments
Post a Comment