当我将 myProg.exe 安装到客户端计算机时,我必须调整 connectionString。
所以我打开文件 MyProg.exe.config,并修改了 connectionString,但它不起作用。
然后我找到了这篇文章。现在的问题是,当我将访问权限修改为公共时,我无法编译/部署它。
我的 App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="FrontEnd_Offline.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.windows.forms jitDebugging="true" />
<connectionStrings>
<add name="Soft8Exp_ClientConnStr" connectionString="Data Source=xxxxx;Initial Catalog=xxxxx;User ID=sa;Password=xxxxx" providerName="System.Data.SqlClient" />
<add name="Soft8Exp_ClientEntities" connectionString="metadata=res://*/Domain.Entite_T.csdl|res://*/Domain.Entite_T.ssdl|res://*/Domain.Entite_T.msl;provider=System.Data.SqlClient;provider connection string="data source=xxxxx;initial catalog=xxxxx;user id=sa;password=xxxxx;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="FrontEnd_Offline.Properties.Settings.Soft8Exp_ClientConnectionString" connectionString="Data Source=xxxxx;Initial Catalog=xxxxx;User ID=sa;Password=xxxxx" providerName="System.Data.SqlClient" />
</connectionStrings>
<applicationSettings>
<FrontEnd_Offline.Properties.Settings>
<setting name="NO_ORDRE" serializeAs="String">
<value />
</setting>
</FrontEnd_Offline.Properties.Settings>
</applicationSettings>
</configuration>
和类连接:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace FrontEnd_Offline.Domain
{
class Connexion : IDisposable
{
public SqlConnection conn;
public SqlConnection GetConnected()
{
try
{
String strConnectionString = ConfigurationManager.ConnectionStrings["Soft8Exp_ClientConnStr"].ConnectionString;
conn = new SqlConnection(strConnectionString);
}
catch (Exception excThrown)
{
conn = null;
throw new Exception(excThrown.InnerException.Message, excThrown);
}
// Ouverture et restitution de la connexion en cours
if (conn.State == ConnectionState.Closed) conn.Open();
return conn;
}
public Boolean IsConnected
{
get { return (conn != null) && (conn.State != ConnectionState.Closed) && (conn.State != ConnectionState.Broken); }
}
public void CloseConnection()
{
// Libération de la connexion si elle existe
if (IsConnected)
{
conn.Close();
conn = null;
}
}
public void Dispose()
{
CloseConnection();
}
}
}