我的项目使用名为informat.xml
的XML文件,其中包含连接数据库所需的所有信息。我在App.config
上的连接字符串中使用它,就像这样
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- things -->
<connectionStrings>
<add name="ServerAS400" connectionString="Data Source=.\informat.xml" />
</connectionStrings>
</configuration>
这可以在我的计算机上运行,但是当远程调用时(到我的Debug文件夹的副本文件夹),它会给我一个错误,说明无法加载XML文件。文件本身位于项目的Debug文件夹中,并作为资源添加到项目属性的资源选项卡中。
编辑:进一步测试让我意识到使用informat.xml
的绝对路径确实可以使它工作。所以问题可能是.\
部分。
触发错误的代码如下
using (Sql400Connection connection = new Sql400Connection(System.Configuration.ConfigurationManager.ConnectionStrings["ServerAS400"].ConnectionString))
try
{
connection.Open(); // ERROR HERE
// some code
connection.Close();
}
catch (Exception e)
{
}
那么,将文件添加到项目中或在App.config
中调用的正确方法是什么?
不需要“informat.xml”。您将所有连接详细信息放在连接字符串本身中。 Server = myServerName \ myInstanceName; Database = myDataBase; User Id = myUsername;密码= MYPASSWORD;