消除连接字符串的硬编码 [英] Eliminate hardcoding of connection string

查看:116
本文介绍了消除连接字符串的硬编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了GUI,可以按 形成对数据库的常规操作(加载现有数据,修改或添加新条目)。 GUI使用原始 WinAPI在 C ++ 中制作。



问题出在连接字符串中。它是硬编码的,因此它强制用户将数据库保存在GUI所在的同一文件夹中。

以下是 C ++ 中的示例,使用 ADO

  static   wchar_t  * bstrConnect = L   Provider = Microsoft.ACE.OLEDB.12.0; \ 
Data Source = .\\MyDatabase.accdb
;



我希望在数据库所在的位置允许用户自由选择,但我不知道如何将应用程序/连接字符串修改为这样做。



我试图在这里搜索一些例子,并在网上搜索但没有成功。也许我是初学者并且自学成才与此有关。



问题:



是否有一个示例/教程/文档可以向我展示处理这种场景的正确原则?

解决方案

不确定如果我正确理解你的问题,但为什么不将数据库位置存储在应用程序所在的同一文件夹中的简单配置(文本)文件中。



第一次应用程序运行时,让用户使用文件打开对话框指定数据库位置,并将该位置存储在配置文件中。我个人不会在这种情况下存储整个连接字符串,只是位置,然后在程序中构建连接字符串。



什么是安全我会使用数据库的数据库密码,当数据库打开时,我会要求用户输入密码。我不会将数据库密码存储在任何地方。



关于数据库密码,请参阅设置数据库密码和取消设置数据库密码 [ ^

I have made GUI that can perform usual operations with the database ( load existing data, modify it or add new entries). GUI is made in C++ using raw WinAPI.

The problem is in connection string. It is hardcoded so it forces the user to keep the database in the same folder where the GUI is.
Below is the example in C++, using ADO:

static wchar_t *bstrConnect= L"Provider=Microsoft.ACE.OLEDB.12.0;\
                                 Data Source = .\\MyDatabase.accdb";


I wish to allow user to have freedom of choice when it comes to where database will be located but I do not know how to modify the application/connection string to do this.

I have tried to search here for examples, and have searched online but had no success. Perhaps me being a beginner and self-taught has something to do with that.

QUESTION:

Is there an example/tutorial/documentation that can show me the correct principle for handling this type of "scenario"?

解决方案

Not sure if I understood your question correctly, but why not store the database location in a simple configuration (text) file in the same folder where the application is.

The first time the application runs, let the user specify the database location with a File Open dialog and store the location in the configuration file. Personally I wouldn't store the whole connection string in this case, just the location and then build the connection string in the program.

What comes to security I'd use a database password for the database and when the database is opened I'd ask the user for the password. I wouldn't store the database password anywhere.

About database password, see Set Database Password and Unset Database Password[^]


这篇关于消除连接字符串的硬编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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