最近做一个小东西,用到了subsonic,因为用的是sqlite,数据库的路径问题又被提了上来。在connectionStrings里面只能指定绝对路径。对于sqlite,access这样的数据库来说,却需要使用到相对路径,否则发布后就会出问题。自己了解的一些解决方案有:
对asp.net,数据库可以放在app_data里面,在web.config里面使用|DataDirectory|来指定路径。
对winform,可以制作安装程序,在安装时生成connectionStrings。因为程序并不大,所以不想制作安装程序。因为决定在程序启动时修改connectionStrings。
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
#region Update //Update the connection config.ConnectionStrings.ConnectionStrings["Northwind"].ConnectionString = "Data Source=" + Application.StartupPath +
                                              @"\Northwind.sl3"; #endregion #region Add  Create a connection string element and  save it to the configuration file. Clear the connection //config.ConnectionStrings.ConnectionStrings.Clear(); //string csName = "Northwind";  Create a connection string element. //ConnectionStringSettings csSettings = //        new ConnectionStringSettings(csName, //        "Data Source=" + Application.StartupPath + @"\Northwind.sl3");  Get the connection strings section. //ConnectionStringsSection csSection = //    config.ConnectionStrings;  Add the new element. //csSection.ConnectionStrings.Add(csSettings); #endregion #region Save // Save the configuration file. config.Save(ConfigurationSaveMode.Modified);
//Refresh ,this is very important. ConfigurationManager.RefreshSection("connectionStrings"); #endregion