部署带有嵌入式sqlite的winform应用程序

发布于 2024-08-29 15:39:56 字数 862 浏览 5 评论 0原文

我正在部署一个使用 vs 2008 0n XP sp3 构建的 winform 应用程序。

我创建了一个带有空架构的数据库,将其放入项目的根文件夹中,并在属性中选择了构建操作嵌入式资源复制到输出目录< /code> :始终复制。现在,我不再在 app.config 连接字符串部分中添加连接字符串,而是在 appSetting 中添加一个条目:key="database";value=" mydb.db;版本=3”。

因此,为了创建我的 connectionString 我使用:

 SQLiteConnection con = new SQLiteConnection("Data Source=" + Path.Combine(Application.StartupPath, ConfigurationManager.AppSettings["database"]));

一切正常,我将应用程序与安装项目打包在一起。现在,在我安装应用程序后,找不到数据库,我不得不将数据库复制到安装项目中的应用程序文件夹使其正常工作。

我的想法是,由于始终复制,数据库应该位于应用程序DLL中。但我无法访问它。那么我到底做错了什么?

我怀疑我应该刚刚连接到根数据库,这意味着不使用 Application.StartupPath

但我在这里询问最佳实践,因为我所做的工作正在工作,但仍然看起来像解决方法,所以请可以有人与我分享他的经验吗? 感谢您的阅读

I'm deploying a winform application built with vs 2008 0n XP sp3.

I created a database with empty schema which i dropped in the root folder of the project and in properties i choosed Build Action: Embedded Resources and Copy to Output directory : Copy always. Now instead of having connectionstring in the app.config connectionString section, i put an entry in appSetting: key="database";value="mydb.db;Version=3".

So to create my connectionString i used :

 SQLiteConnection con = new SQLiteConnection("Data Source=" + Path.Combine(Application.StartupPath, ConfigurationManager.AppSettings["database"]));

Everything works fine and i packaged the app with a setup project.Now after i installed the app the database could not be found and i was obliged to copy the database to the Application Folder in the setup project for it to work.

what i thought is that db is supposed to be in the app dll because of copy always .but i can't access it.So what exactly did i do wrong?

i'm suspecting i should have just connected to the root db meaning not using Application.StartupPath

But i'm here asking for the best practices cause what i did is working but still looking like workaround so please can anyone share his experience with me?
thanks for reading

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

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

发布评论

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

评论(1

墨小沫ゞ 2024-09-05 15:39:56

Embedded Resource 意味着数据库嵌入到您的 dll 中。 复制到输出目录 设置在这种情况下不适用,该设置用于构建操作:内容

嵌入数据库后,您基本上必须在第一次使用时取消嵌入它。为此,请从程序集中读取它并将其存储到文件中。

class EmbeddedResourceTest
{
    public static void Test()
    {
        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Test.db");

        using(var resourceStream = typeof(EmbeddedResourceTest).Assembly.GetManifestResourceStream("Test.db"))
        {
            using(var fileStream = File.OpenWrite(path))
            {
                CopyStream(resourceStream, fileStream);
            }
        }

        // now access database using 'path'
    }

    public static void CopyStream(Stream inputStream, Stream outputStream)
    {
        CopyStream(inputStream, outputStream, 4096);
    }

    public static void CopyStream(Stream inputStream, Stream outputStream, int bufferLength)
    {
        var buffer = new byte[bufferLength];
        int bytesRead;
        while ((bytesRead = inputStream.Read(buffer, 0, bufferLength)) > 0)
        {
            outputStream.Write(buffer, 0, bytesRead);
        }
    }
}

Embedded Resource means the database gets embedded in your dll. The Copy to output directory setting doesn't apply in this case, that's used for Build Action: Content.

With the database embedded, you basically have to un-embed it on first use. To do this read it out of the Assembly and store it to a file.

class EmbeddedResourceTest
{
    public static void Test()
    {
        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Test.db");

        using(var resourceStream = typeof(EmbeddedResourceTest).Assembly.GetManifestResourceStream("Test.db"))
        {
            using(var fileStream = File.OpenWrite(path))
            {
                CopyStream(resourceStream, fileStream);
            }
        }

        // now access database using 'path'
    }

    public static void CopyStream(Stream inputStream, Stream outputStream)
    {
        CopyStream(inputStream, outputStream, 4096);
    }

    public static void CopyStream(Stream inputStream, Stream outputStream, int bufferLength)
    {
        var buffer = new byte[bufferLength];
        int bytesRead;
        while ((bytesRead = inputStream.Read(buffer, 0, bufferLength)) > 0)
        {
            outputStream.Write(buffer, 0, bytesRead);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文