[英]How do I make a non absolute connection string so my small database works with my published C# application?
I'm working on a small project to track information. 我正在做一个跟踪信息的小项目。 This C# application will take information uploaded via CSV/Excel and store/sort it. 此C#应用程序将获取通过CSV / Excel上传的信息并进行存储/排序。
My current connection string is an absolute path (off thumb drive). 我当前的连接字符串是绝对路径(关闭拇指驱动器)。 I'm worried that when I publish it, the database connection won't work on a random users computer. 我担心发布该数据库时,数据库连接在随机用户计算机上将无法正常工作。
<add name="PPP_Project.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Other PPP Projects\PPP_Project_Test\PPP_Project\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
How do I have this set so it will work when the application is published? 我如何设置此设置以便它在发布应用程序时起作用?
you can use this: 您可以使用此:
Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|Customers.mdf;Integrated Security=True;User Instance=True
as described here . 作为描述在这里 。 For a winform application, the default |DataDirectory| 对于Winform应用程序,默认| DataDirectory | is the same where the application is installed. 与安装应用程序的位置相同。 Of course you can use aa a part of a more nested path. 当然,您可以使用更多嵌套路径的一部分。 If you want to specifyng something else you can use the AppDomain.SetData
method. 如果要指定其他内容,可以使用AppDomain.SetData
方法。
The easiest way to manipulate connection strings is with the ConnectionStringBuilder classes: 处理连接字符串的最简单方法是使用ConnectionStringBuilder类:
OleStringBuilder = new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
OleStringBuilder.DataSource = MapPath(@"~\App_Datav\MyExcelWorksheet.xls");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.