如何为不同的目录驱动器保存到Access数据库 [英] How to save to an Access Database for different directory drives

查看:86
本文介绍了如何为不同的目录驱动器保存到Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#2008,Windows Forms,Access 2010数据库。

我有一个表单收集数据,然后保存到我的计算机上的目录... C:\\ drive。我最终将这个应用程序安装在不同的机器上,并将数据保存到我们服务器上的目录中。此目录可以在一台机器上设置为X驱动器,在另一台机器上设置为Z驱动器。我该怎么做呢?我当前的连接是这样写的:

C# 2008, Windows Forms, Access 2010 database.
I have a form that collects data and then saves to a directory on my computer..C:\\ drive. I am going to eventually have this application installed on different machines and have the data saved to a directory on our server. This directory may be set as the X drive on one machine and the Z drive on another. How do I do this? my current connection is written as so:

OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Temp\\Test.accdb");

推荐答案

最简单的方法是将数据库路径作为应用程序中的可配置项,并引用该值来构建连接字符串。

如果配置项不可用,请使用应用程序定义的默认路径。



一些样本

如何从App获取连接字符串.Config in c# [ ^ ]



提供设置UI t o如果用户希望稍后更改此路径,请修改此路径。
The simplest way would be having the database path as a configurable item in your application and refer that value to build your connection string.
If the config item is not available use a default path defined by your application.

some samples
How to get Connection String from App.Config in c#[^]

provide a settings UI to modify this path by the users if they want to change this path later time.


您是否考虑过将数据库与客户端 - 服务器架构一起使用?它甚至可以是轻量级的,而不像Microsoft Access那样专有。你看,基于文件(或基于文件服务器)的访问将无处可去。

这是你可以看到的第一个问题,进一步的问题可能会困扰你。



请参阅:

http://en.wikipedia.org/wiki / Microsoft_Access [ ^ ],

http://en.wikipedia.org/wiki/Client-server [ ^ ]。



-SA
Did you ever think on using database with client-server architecture? And it even can be light-weight and not proprietary as Microsoft Access is. You see, file-based (or fileserver-based) Access will lead you nowhere.
This is the first problem you can see, further problems may haunt you badly.

Please see:
http://en.wikipedia.org/wiki/Microsoft_Access[^],
http://en.wikipedia.org/wiki/Client-server[^].

—SA


在运行时构建连接字符串。

使用 GetEntryAsembly [ ^ ]它是位置 [ ^ ]属性。



Build your connection string at runtime.
Use GetEntryAssambly[^] and it''s Location[^] property.

using System;
using System.IO;
using System.Reflection;

namespace My
{
	class Program    
	{
		public static void Main()
		{
			string folder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
			string filename = Path.Combine(folder, "text.accdb");
			string connectiostring = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}",filename);
			
			Console.WriteLine(connectiostring);
		}
	}
}


这篇关于如何为不同的目录驱动器保存到Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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