简体   繁体   English

如何创建非绝对连接字符串,以便我的小型数据库可以与已发布的C#应用​​程序一起使用?

[英]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.

相关问题 如何编写外部连接字符串配置文件并使我的C#应用​​程序识别并读取它? - How do I write an external connection string config file and make my c# application recognize and read it? 如何使用Access数据库使我的C#应用​​程序在LAN上使用一个数据库运行 - how do i make my C# application using Access Database to run on LAN using one database 如何找到通过C#应用程序自动执行SAP所需的连接字符串? - How do I find my Connection String required to automate SAP through a C# application? 必须使用 SQL Server 数据库为我的 C# 应用程序创建连接字符串 - Have to make a connection string for my C# Application with SQL Server database 当我在客户端PC中安装连接字符串时,我需要在c#应用程序中编辑连接字符串吗,如果可以,我该怎么做? - do i need to edit connection string in my c# application when i install it in a client pc, if yes how can i do it? 在C#中,如何正确重载类中的Equals运算符,以便Queue.Contains()工作? - In C#, how do I overload the Equals operator in my class properly so that Queue.Contains() works? 如何将Acces数据库添加到我发布的C#程序中? - How to add an Acces Database to my published C# program? C# ASP.Net 我如何将我的 sqlite 数据库连接到 home controller - C# ASP.Net How do I give my sqlite database connection to the home controller 如何使我的连接字符串在 C# 中通用,也就是如何使它对我的应用程序的每个用户都可用? - How to make my Connection string universal in C#, aka how to make it usable for every user of my app? 如何在azure中为我的Web应用程序设置连接字符串? - how do i set the connection string for my web application in azure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM