设置SQL Server数据库连接的目录(连接字符串)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设置SQL Server数据库连接的目录(连接字符串)相关的知识,希望对你有一定的参考价值。
This snippet is actually tested for SQL Server Compact Edition (CE) 3.5, but it will probably work for other SQL Server editions. Once you have an SQL CE db up and running, you can set the connection string to point to any directory and database by simply using the |DataDirectory| string in the dbConnectionString Property (created by default when you create the database connection) and then relocating the location to which |DataDirectory| points by calling AppDomain.CurrentDomain.SetData("DataDirectory", @"C:PathToDB"). Since the dbConnectionString property appends dbName.sdf, we leave that part off of the SetData argument.The code below sets the data directory to the common application data folder (usually C:Documents and SettingsAll UsersApplication Data on winXP). It seems to work fine when invoked in the Form_Load method.
Form_Load(object sender, EventArgs e) { // Set the db directory to the common app data folder AppDomain.CurrentDomain.SetData("DataDirectory", System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData)); }
以上是关于设置SQL Server数据库连接的目录(连接字符串)的主要内容,如果未能解决你的问题,请参考以下文章
指定连接字符串的 SQL Server Compact 4.0 代码优先