列出将在视图中显示的数据库文件的内容

本文关键字:数据库 文件 显示 视图 | 更新日期: 2023-09-27 18:12:00

我使用c#编写的开源指纹识别程序(SourceAFIS),该程序将注册用户存储在.dat文件中。

从。dat文件中读取,使用:

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); 
}

其中Database为database.dat文件的路径。

有了这个,我可以使用database.Add(Enroll(Image, Name));将人们登记到系统中。

登记函数如下:

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;
}

我想知道是否有可能列出数据库中的每个用户,并在我的一个ASP中显示它们。. NET MVC视图?我如何能够检查写入数据库的方式?

编辑:

在控制器中声明的MyPerson和MyFingerprint类
[Serializable]
class MyPerson : Person
{ public string Name; }
[Serializable]
class MyFingerprint : Fingerprint
{ public string Filename; }

列出将在视图中显示的数据库文件的内容

示例中的database对象不是传统意义上的数据库。相反,它是序列化为文件的MyPerson对象的List,包括它们的指纹模板和MyPerson类包含的任何其他值。有一个文档描述了用于写入文件的格式,但由于该文件仅表示List<MyPerson>类型的对象,因此尝试读取该文件的二进制格式并没有多少好处。我已经多次使用序列化,但从来没有费心去查看文件格式。让BinaryFormatter类处理这些工作要安全得多。

如果你还没有,你应该看看MSDN上序列化的文档。如果您的MyPerson类将来有可能被更改,您应该查看关于版本容忍序列化的部分,以避免将来可能出现的问题。

database对象可以直接传递给视图,如下面的示例代码所示。您可能需要更新视图中的名称空间以反映您的项目。Index方法中的代码只是为了演示这个概念:它不能反映好的应用程序架构。

控制器

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);
    }
}

视图,只列出Name属性

@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>

您已经使用SourceAFIS中的Sample项目作为您的基础。您最好使用真正的数据库来存储人物和图像信息。与基于文件的存储相比,这有太多的优点。

  • 允许多个客户端
  • 修复并发问题
  • 提供网络访问
  • 提供简单的分页
  • 不需要文件系统访问

您甚至可以使用SQL Server Express版开始。

现在,回答你的问题

  1. 是的,你可以在你的MVC页面上列出你的用户,与他们并排的图片。
  2. 您不需要检查用户写入的"数据库",因为所有用户都存储在同一个数据库中,您可以使用discriminator字段来过滤您想要的用户。

你注意到你的知识是有限的。你应该开始通过研究和阅读来扩展你的知识。你可以开始阅读有关实体框架或Linq 2 Sql的内容。

创建数据库并将数据文件加载到该数据库后,代码将如下所示(使用Linq2Sql):

public class MyPersonController : Controller {
    public ActionResult Index() {
        var people = PersonStore.GetAllPeople();
        return View(people);
    }
    public ActionResult Image(int id) {
        var image = PersonStore.GetPersonImageById(id);
        return BinaryResult(image.Content, image.MimeType);
    }
}

和你的视图

@model IEnumerable<PersonModel>
<table>
    @foreach (var Person in Model)
    {
        <td>@person.Name</td>
        <td><img src="@Url.Action("Image", new { id = person.id })"></td>
    }
</table>