使用嵌入式sqlite部署winform应用程序 [英] deploying winform application with embedded sqlite

查看:412
本文介绍了使用嵌入式sqlite部署winform应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我创建了一个空模式的数据库,我将它放在项目的根文件夹中,并在我选择的属性中 Build Action 嵌入资源复制到输出目录始终复制。现在,在app.config connectionString部分中没有连接字符串,我在 appSetting 中输入一个条目: key =database ; = mydb.db;版本= 3\" 。



所以要创建我的 connectionString 我使用:

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

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



我以为是因为复制总是的数据库应该在应用程序中,但是我可以不知道我做错了什么?



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



但是我在这里要求最佳做法,因为我做的是工作,但仍然看起来像解决方案,所以请任何人都可以与我分享他的经验?
感谢阅读

解决方案

嵌入式资源表示数据库嵌入你的dll。在这种情况下,复制到输出目录设置不适用,用于 Build Action:Content



嵌入数据库时​​,您首先必须将其嵌入。要做到这一点,将其从Assembly中读出并存储到一个文件中。

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

//现在使用'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);
}
}
}


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

解决方案

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

这篇关于使用嵌入式sqlite部署winform应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆