从 db4o 数据库加载对象

发布于 2024-08-19 08:10:03 字数 1668 浏览 3 评论 0原文

我正在开发一个使用 db4o 作为后端的电子商务网站。一切都很顺利,直到上周我遇到了一个我一直无法解决的问题。下面的代码非常简单。我打开一个数据库文件,保存一个对象,然后尝试检索它。然而我什么也没得到。 “users”变量的计数为零。

public class Program
{
    private static string _connectionString = string.Format(@"c:\aaarrrr.db4o");

    static void Main(string[] args)
    {
        TestUser container = new TestUser() { id = 1, Name = "Mohammad", Surname = "Rafiq" };

        Db4oFactory.Configure().Diagnostic().AddListener(new DiagnosticToConsole());

        using (var dbc = Db4oFactory.OpenFile(_connectionString))
        {
            dbc.Store(container);
        }

        IList<TestUser> users = null;

        using (var dbc = Db4oFactory.OpenFile(_connectionString))
        {
            users = dbc.Query<TestUser>(x => x.id == 1).ToList();
        }

        if (users.Count > 0)
        {
            Console.WriteLine("{0} {1} with id of {2}", users.First().Name, users.First().Surname, users.First().id);
        }
        else
        {
            Console.WriteLine("\nNo data returned.");
        }

        Console.ReadLine();
    }
}

public class TestUser
{
    [Indexed]
    private int _id = 0;
    private string _name = string.Empty;
    private string _surname = string.Empty;

    public int id { get { return _id; } set { _id = value; } }
    public string Name { get { return _name; } set { _name = value; } }
    public string Surname { get { return _surname; } set { _surname = value; } }
}

我已附加 db4o 诊断侦听器,但在控制台输出中看不到任何内容。一切看起来都很好。我知道我正在写入文件,因为我可以看到文件大小增加并且时间戳也更新。我已经检查了所有项目设置,它们都设置为默认值。我正在使用 .net 4、Visual Studio 2010 beta 和 Windows 7。我已经阅读了一些有关反射权限的内容,但我看不出这在这里如何应用。任何帮助或想法将不胜感激。

I am developing an e-commerce website that utilises db4o as the backend. All was well until last week when I came across a problem that I have been unable to solve. The code below is quite straight forward. I open a database file, save an object and then try to retrieve it. However I get nothing back. The "users" variable has a count of zero.

public class Program
{
    private static string _connectionString = string.Format(@"c:\aaarrrr.db4o");

    static void Main(string[] args)
    {
        TestUser container = new TestUser() { id = 1, Name = "Mohammad", Surname = "Rafiq" };

        Db4oFactory.Configure().Diagnostic().AddListener(new DiagnosticToConsole());

        using (var dbc = Db4oFactory.OpenFile(_connectionString))
        {
            dbc.Store(container);
        }

        IList<TestUser> users = null;

        using (var dbc = Db4oFactory.OpenFile(_connectionString))
        {
            users = dbc.Query<TestUser>(x => x.id == 1).ToList();
        }

        if (users.Count > 0)
        {
            Console.WriteLine("{0} {1} with id of {2}", users.First().Name, users.First().Surname, users.First().id);
        }
        else
        {
            Console.WriteLine("\nNo data returned.");
        }

        Console.ReadLine();
    }
}

public class TestUser
{
    [Indexed]
    private int _id = 0;
    private string _name = string.Empty;
    private string _surname = string.Empty;

    public int id { get { return _id; } set { _id = value; } }
    public string Name { get { return _name; } set { _name = value; } }
    public string Surname { get { return _surname; } set { _surname = value; } }
}

I have attached db4o diagnostic listener and I see nothing in the console output. Everything seems fine. I know I am writing to the file because I can see the file size increase and the timestamp is also updated. I have checked all the project settings and they are all set to default. I am using .net 4, visual studio 2010 beta and windows 7. I have done some reading regarding reflection permission but I cant see how this applies here. Any help or ideas would be knidly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め可乐爱微笑 2024-08-26 08:10:04

调用 store() 后,您需要在离开 using{} 语句之前提交 commit()。您在提交更改之前关闭了数据库。

After calling store(), you need to commit() before leaving the using{} statement. You closed your database before committing your changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文