NET中SQLSERVER COMPACT SDF、SQLITE数据库相对路径设置方法
You should use:Data Source=|DataDirectory|\MyDb.sdf
|DataDirectory| points to the App_Data folder.
程序启动时,设置 AppDomain.CurrentDomain.setData("DataDirectory", my directory string); Winform的程序当然是在Main函数中,不废话,我的代码如下
stringp=AppDomain.CurrentDomain.BaseDirectory; if(p.IndexOf( " \\bin\\ " )> 0 ) { if (p.EndsWith("\\bin\\Debug\\")) p = p.Replace("\\bin\\Debug", ""); if (p.EndsWith("\\bin\\Release\\")) p = p.Replace("\\bin\\Release", ""); } if( ! p.EndsWith( " App_Data\\ " )) p=p+ " App_Data\\ " ; AppDomain.CurrentDomain.SetData( " DataDirectory " , p);
思路是去掉调试期和生产期的区别,所以需要注意的是,程序发布的时候,数据库也要放在App_Data目录下。
(You should use:
Data Source=|DataDirectory|\MyDb. sdf
|DataDirectory| points to the App_ Data folder.
When the program starts, set AppDomain CurrentDomain. setData("DataDirectory", my directory string);Of course, the program of WinForm is in the main function. No nonsense. My code is as follows
stringp=AppDomain. CurrentDomain. BaseDirectory; if(p.IndexOf( " \\bin\\ " )> 0 ) { if (p.EndsWith("\\bin\\Debug\\")) p = p.Replace("\\bin\\Debug", ""); if (p.EndsWith("\\bin\\Release\\")) p = p.Replace("\\bin\\Release", ""); } if( ! p.EndsWith( " App_Data\\ " )) p=p+ " App_Data\\ " ; AppDomain. CurrentDomain. SetData( " DataDirectory " , p);
The idea is to remove the difference between debugging period and production period, so it should be noted that when the program is released, the database should also be placed in app_ Data directory.
)
页:
[1]