我在C#解决方案中有一个VS2012,包含4层结构的4个项目(Presentation,BusinessLogic,DomainModel,DataAccess),并希望为用户提供在Login表单中选择数据库文件路径的选项,该表单位于Presentation中layer(当在DataAccess层中的静态方法中创建与Database的连接时使用它)。并且路径将被保存并在下次应用程序运行时使用。
更多的工作流程示例如下:
Codewise我不想为我需要创建连接等的每个方法在我的代码周围传递该字符串(路径)。
那么关于如何直接将其保存在直接使用它的方法中的任何想法?如果不仅仅是当用户想要更改它时,那么只需强制用户在启动应用程序时选择文件。
目前我正在做的是在他想要使用另一个数据库文件时强制用户在启动应用程序之前将他想要的文件放在解决方案的目录中并使用特定名称。为此我正在使用:
public static string path;
public static OleDbConnection createConnection()
{
path = DirProject() + "\\Calibrações Equipamentos ULSM.accdb";
OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
builder["Provider"] = "Microsoft.ACE.OLEDB.12.0";
builder["Data Source"] = path;
return new OleDbConnection(builder.ConnectionString);
}
private static string DirProject()
{
string DirDebug = System.IO.Directory.GetCurrentDirectory();
string DirProject = DirDebug;
for (int counter_slash = 0; counter_slash < 3; counter_slash++)
{
DirProject = DirProject.Substring(0, DirProject.LastIndexOf(@"\"));
}
return DirProject;
}
答案 0 :(得分:1)
配置(=数据)不保存到方法(=代码)中。它通常保存在配置文件中。您可以利用.NET's application and user settings mechanism来实现您的目标。
答案 1 :(得分:1)