如何在连接字符串中添加数据目录以在客户端机器上部署应用程序?
How to add data directory in connection string to deploy application on client machine?
我在开发过程中用 c# 创建了一个 windows 表单应用程序,我使用了以下连接字符串并且它工作正常
<connectionStrings>
<add name="myconnection" connectionString="Data Source=ABC-PC\SQLEXPRESS;Initial Catalog=mydatabase;Integrated Security=True"/>
</connectionStrings>
但现在我需要在客户端机器上部署应用程序,我必须在我的连接字符串中添加数据目录选项,我是这样做的
<connectionStrings>
<add name="myconnection" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True; User Instance=True;AttachDbFilename=|DataDirectory|\mydatabase.mdf; Initial Catalog=mydatabase; "/>
</connectionStrings>
当我更改字符串时,它抛出了错误
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'.
并将连接字符串调用为
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
我使用 SQL 服务器 2008。
经过大量研究和尝试,我找到了连接字符串正确的解决方案,我所做的一件事是将数据库文件从 SQL 服务器的文件夹复制到项目文件夹。现在它按照我的要求工作正常。
我在开发过程中用 c# 创建了一个 windows 表单应用程序,我使用了以下连接字符串并且它工作正常
<connectionStrings>
<add name="myconnection" connectionString="Data Source=ABC-PC\SQLEXPRESS;Initial Catalog=mydatabase;Integrated Security=True"/>
</connectionStrings>
但现在我需要在客户端机器上部署应用程序,我必须在我的连接字符串中添加数据目录选项,我是这样做的
<connectionStrings>
<add name="myconnection" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True; User Instance=True;AttachDbFilename=|DataDirectory|\mydatabase.mdf; Initial Catalog=mydatabase; "/>
</connectionStrings>
当我更改字符串时,它抛出了错误
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'.
并将连接字符串调用为
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
我使用 SQL 服务器 2008。
经过大量研究和尝试,我找到了连接字符串正确的解决方案,我所做的一件事是将数据库文件从 SQL 服务器的文件夹复制到项目文件夹。现在它按照我的要求工作正常。