如何在连接字符串中添加数据目录以在客户端计算机上部署应用程序

时间:2022-07-13 15:44:59

I created an windows form application in c# during development i used following connection string and it works fine

我在开发期间使用以下连接字符串在c#中创建了一个Windows窗体应用程序,它工作正常

<connectionStrings>
    <add name="myconnection" connectionString="Data Source=ABC-PC\SQLEXPRESS;Initial Catalog=mydatabase;Integrated Security=True"/>
  </connectionStrings>

but now i need to deploy application on client machine and i have to add data directory option in my connection string and i did this as

但现在我需要在客户端机器上部署应用程序,我必须在我的连接字符串中添加数据目录选项,我这样做了

<connectionStrings>
   <add name="myconnection" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True; User Instance=True;AttachDbFilename=|DataDirectory|\mydatabase.mdf;  Initial Catalog=mydatabase; "/>
  </connectionStrings>

when i changed string then it threw and error

当我改变字符串然后它扔了错误

Unable to open the physical file "D:\Other Projects\Employee\Employee\bin\Debug\mydatabase.mdf". Operating system error 2: "2(The system cannot find the file specified.)". Cannot attach the file 'D:\Other Projects\Employee\Employee\bin\Debug\mydatabase.mdf' as database 'mydatabase'.

无法打开物理文件“D:\ Other Projects \ Employee \ Employee \ bin \ Debug \ mydatabase.mdf”。操作系统错误2:“2(系统找不到指定的文件。)”。无法将文件'D:\ Other Projects \ Employee \ Employee \ bin \ Debug \ mydatabase.mdf'作为数据库'mydatabase'附加。

and calling connection string as

并将连接字符串调用为

SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);

I used SQL server 2008.

我使用的是SQL Server 2008。

1 个解决方案

#1


0  

After a lot of research plus hit an try i found solution that connection string is correct a thing that i did is to copy database files from SQL server's folder to project folder. Now it is working fine according to my requirement.

经过大量的研究加上尝试,我发现连接字符串是正确的解决方案,我做的是将数据库文件从SQL服务器的文件夹复制到项目文件夹。现在它根据我的要求正常工作。

#1


0  

After a lot of research plus hit an try i found solution that connection string is correct a thing that i did is to copy database files from SQL server's folder to project folder. Now it is working fine according to my requirement.

经过大量的研究加上尝试,我发现连接字符串是正确的解决方案,我做的是将数据库文件从SQL服务器的文件夹复制到项目文件夹。现在它根据我的要求正常工作。