zoukankan      html  css  js  c++  java
  • 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.

    查看:

    https://stackoverflow.com/questions/12266924/how-do-i-read-the-current-path-of-datadirectory-from-config-settings

    作者:旭东
    出处:http://www.cnblogs.com/HQFZ
    关于作者:专注于微软平台项目架构、管理和企业解决方案。现主要从事WinForm、ASP.NET、WPF、WCF、等方面的项目开发、架构、管理。如有问题或建议,请不吝指教!
    本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以联系我,非常感谢。
    如果您该文觉得不错或者对你有帮助,请点下推荐,让更多的朋友看到,谢谢!

  • 相关阅读:
    Duang~Duang~Duang 还在使用jsfiddle和jsbin做在线前端代码展示和演示吗? 试试更强大的在线代码分享工具吧!
    如数据库一般访问互联网资源
    HTML5来了,7个混合式移动开发框架
    Three.js纹理贴图正方体旋转动画效果
    极客Web开发资源大荟萃#003
    精彩代码回放:jQuery实现的浏览器类型和版本检测
    响应式的全屏背景图片效果
    Delphi多线程编程之同步读写全局数据
    delphi与sqlite
    Delphi调用IE打开网页
  • 原文地址:https://www.cnblogs.com/HQFZ/p/4112233.html
Copyright © 2011-2022 走看看