C#WinForm应用程序-如何保存连接字符串(SQLite) [英] C# WinForm application - How to save connection string (SQLite)

查看:263
本文介绍了C#WinForm应用程序-如何保存连接字符串(SQLite)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有数据库(SQLite)的.NET 4.0中有一个winform应用程序。我需要为用户添加一个选项,以便能够更改包含数据库的文件的路径-简而言之,就是更改连接字符串。我使用app.config保存这样的连接字符串:

 < connectionStrings> 
<添加name = connectionString connectionString = Data Source = D:\myDatabase.db; FailIfMissing = True; Version = 3 />
< / connectionStrings>

有没有一种方法允许用户修改app.config文件(位于Program Files文件夹中) )?我不想以管理员身份运行该应用程序,是否有一种方法可以仅在尝试修改app.config文件时临时授予该用户管理员权限?



如果您对于持久存储连接字符串有更好的建议,请分享。



谢谢!



编辑:
我实际上可以更改app.config文件:

  System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None ); 
config.ConnectionStrings.ConnectionStrings [ connectionString]。ConnectionString =数据源= +路径+; FailIfMissing = True;版本= 3;
config.Save();
ConfigurationManager.RefreshSection( connectionStrings);

问题是当用户不是管理员时,对文件的访问被拒绝。有没有解决方法(我不想以管理员身份运行该应用程序)?还是有更好的方法来存储我的连接字符串?

解决方案

我认为您要使用的是设置(而不是使用app.config)。请参阅: https://msdn.microsoft .com / en-us / library / aa730869%28v = vs.80%29.aspx



它与配置文件基本相同,但是给出了用户(和您)轻松访问。



首先在设计器中设置默认的连接字符串:

然后像 textBox1.Text = Properties.Settings.Default.connectionString;



执行相反的操作以保存它: Properties.Settings.Default.connectionString = textBox1.Text;



并确保保存: Properties.Settings.Default.Save();


I have a winform app in .NET 4.0 with database (SQLite). I need to add an option for user to be able to change the path to the file containing the database - in short, to change the connection string. I used app.config to save connection string like this:

<connectionStrings>
<add name="connectionString" connectionString="Data Source=D:\myDatabase.db; FailIfMissing=True; Version=3"/>
</connectionStrings>

Is there a way to allow user to modify app.config file (it is located in Program Files folder)? I do not want to run the app as an administrator, is there a way to give administrator rights to user temporarely, only when trying to modify app.config file?

If you have any better suggestion for persistently storing connection string, please share.

Thank you!

EDIT: I can actually change app.config file:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = "Data Source=" + path + "; FailIfMissing=True; Version=3";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");

Problem is when user is not an administrator, access to the file is denied. Is there a workaround (I do not want to run the app as administrator)? Or is there a better way to store my connection string?

解决方案

I think what you want to do is use Settings (instead of using the app.config). See this: https://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx

It's basically the same as the config file, but gives the user (and you) easy access.

First set a default connection string in the designer:

Then access it like textBox1.Text = Properties.Settings.Default.connectionString;

Do the reverse to save it: Properties.Settings.Default.connectionString = textBox1.Text;

And make sure you save: Properties.Settings.Default.Save();

这篇关于C#WinForm应用程序-如何保存连接字符串(SQLite)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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