ADO.net 连接字符串中的 |DataDirectory| 是什么
|DataDirectory|
does not come from config settings; you're mixing up three different things:
ConfigurationManager.AppSettings["DataDirectory"]
This comes from config settings; a .config file you have to create and put into your project. This particular setting is the value of the element with key "DataDirectory"
in the AppSettings
element. This doesn't exist unless you put one in the .config file. Typically this is where you put configuration or startup data that is never changed. You should not put file paths here, as they can be different on the machine users install your database to.
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
This is the path to the current user's roaming application data folder defined by the operating system your app was installed on. You cannot change this, it is defined by the OS. You can be sure this folder is writable by the user, and will be available if the user roams, or logs on from another machine. This is typically where you want to put editable user data.
SqlConnection("Data Source=|DataDirectory|\\DatabaseFileName.sdf;...")
This is a connection string for an ADO.NET connection. ADO.NET treats vertical bars specially, it looks for an AppDomain data matching the key name between the vertical bars. You can get the same data with:
AppDomain.CurrentDomain.GetData("DataDirectory")
So what writes the value of DataDirectory
? It's done by whatever deploys your executable:
- .MSI installers define it as the app's target folder.
- ClickOnce defines a special data folder in your project.
- Web apps use the App_Data folder.
- The Visual Studio debugger uses the debug folder.
Note that .MSI installers can allow the user to change the DataDirectory; this is why you should never hard-code or change DataDirectory
, if you do that there is no way to find where your application data was deployed. You typically use the DataDirectory
folder for read-only binary data deployed with your executable.
If you need to write to the data deployed with your executable you should first copy it someplace you know the user will be able to write to, such as to Environment.SpecialFolder.ApplicationData
, and write to the copy. Not only is DataDirectory
not necessarily writable by users, it is part of the deployment and not part of the user data; if you repair or uninstall your executable then DataDirectory
gets reinstalled or deleted. Users don't like it when you delete their data, so don't save it to DataDirectory
.
查看:
ADO.net 连接字符串中的 |DataDirectory| 是什么的更多相关文章
- 转:ADO.NET连接字符串
名称 ADO.NET连接字符串 说明 ADO.NET连接字符串:SQL Server,SQL Server 2005,ACCESS,Oracle,MySQL,Interbase,IBM DB2,Syb ...
- 连接字符串中Min Pool Size的理解是错误,超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
Min Pool Size的理解是错误的 假设我们在一个ASP.NET应用程序的连接字符串中将Min Pool Size设置为30: <add name="cnblogs" ...
- ADO.NET连接字符串大全---各种数据库的连接字符串
ADO.NET连接字符串大全 ADO.NET连接字符串 名称 ADO.NET连接字符串 说明 ADO.NET连接字符串:SQL Server,SQL Server 2005,ACCESS,Oracle ...
- ADO.NET连接字符串大全
说明ADO.NET连接字符串:SQL Server,SQL Server 2005,ACCESS,Oracle,MySQL,Interbase,IBM DB2,Sybase,Informix,Ingr ...
- [穷尽]ADO.NET连接字符串
微软提供的四种数据库连接方式: System.Data.OleDb.OleDbConnection System.Data.SqlClient.SqlConnection System.Data.Od ...
- 【转载】两个Web.config中连接字符串中特殊字符解决方案
userid = test password = aps'"; 那么连接字符串的写法为: Provider=SQLOLEDB.1;Password="aps'"&quo ...
- db2 jdbc连接字符串中 指定currentSchema
场景:连接DB2数据库的,jdbc的连接字符串中没有给当前的数据源用户指定默认的schema,而当前的数据源用户下可能有多个schema,则会使用数据源用户默认的schema. 例如:admin用户的 ...
- Ado.net连接字符串
学习刘皓的 文章ADO.NET入门教程(三) 连接字符串,你小觑了吗? 连接字符串主要有DataSource 指定地址 通常是ip 如果Express 就要使用形如 ./Express或者 (loca ...
- 关于打开现有项目时数据库连接配置遇到的问题 连接字符串中的数据源值指定未安装的SQL Server的实例。要解决此问题,可选择安装匹配的SQL Server实例或修改连接字符串中的数据源值
最近在看红皮书<ASP.NET MVC 5 高级编程>时,为了更好理解,边看书,边打开源代码查看,在VS(Visual Studio 2015)中将源代码打开,发现数据库连接是关闭的,本想 ...
随机推荐
- 用NPOI从DataBase到Excel '2
NPOI的C# Helper代码2 public static MemoryStream ExportXls(DataTable dt) { HSSFWorkbook wk = new HSSFWor ...
- VS2012 C#使用/配置Log4Net
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本节探讨如何在VS2012中使用Log4Net 1. ...
- 【C#】 异常处理
异常是在程序执行期间出现的问题.C# 中的异常是对程序运行时出现的特殊情况的一种响应,比如尝试除以零等异情况. 异常提供了一种把程序控制权从某个部分转移到另一个部分的方式. C# 异常处理时建立在四个 ...
- css基本知识框架图
css基本知识框架:(一:基本知识缩影.二基本知识框架图) 1.css样式表的基本概念 2.样式表基本类型-----1.内嵌样式 2.内联样式3.链入外部样式表4.导入外部样式 3.样式表配置方法 4 ...
- 基于 Eclipse 的 MapReduce 开发环境搭建
文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/6055850.html 上周末本来要写这篇的,结果没想到上周末自己环境都没有搭起来,运行起 ...
- jquery给div的innerHTML赋值
$("#id").html()=""; //或者 $("#id").html("test");
- 移动H5前端性能优化指南
移动H5前端性能优化指南 概述 1. PC优化手段在Mobile侧同样适用2. 在Mobile侧我们提出三秒种渲染完成首屏指标3. 基于第二点,首屏加载3秒完成或使用Loading4. 基于联通3G网 ...
- jQuery实现侧边导航栏效果
效果图: 效果体验:http://keleyi.com/keleyi/phtml/jqmenu/2.htm 以下是完整代码: <!DOCTYPE html> <html> &l ...
- JS中的函数(二):函数参数(你可能不知道的参数传递)
前言: 函数分为有参有返回值,有参无返回值,无参无返回值,无参有返回值:那么对于无参数的函数你想使用函数的调用怎么办呢?如果你想封装一个代码,实现多种功能,但是形参大于实参或者实参大于形参又该如何?本 ...
- 基于git diff进行的eslint代码检测
缘起 在项目中, 通常都会使用代码检测工具来规范团队的代码风格, 比如eslint.随着代码的不断增加, eslint进行代码检测的时间也越来越久.每次检测的时候, 需要检测的文件和实际检测的文件极度 ...