当我将myProg.exe安装到客户端计算机时,我必须调整connectionString。
所以我打开文件MyProg.exe.config,并修改了connectionString,但它不起作用。
然后我找到了article。现在当我将访问权限修改为公共时,我无法编译/部署它。
我的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();
}
}
}
答案 0 :(得分:1)
您没有说您在开始时使用的是ClickOnce。情况有所不同:配置文件存储在用户的配置文件中。您无法更改文件(仅通过应用程序进行用户设置)。
要使用新的连接字符串更新ClickOnce程序,我建议如下:
true
将以下代码添加到应用程序的启动代码(Program.cs或App.cs)
if (Properties.Settings.Default.SettingsUpgradeNeeded)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.SettingsUpgradeNeeded = false;
Properties.Settings.Default.Save();
}
发布新的更新。
更改连接的另一种方法是(如果您在代码中仅使用SqlConnection
,则更为可取):
ConnectionStrings
部分SqlConnectionStringBuilder
从这些设置 修改强>
您希望用户能够更改连接参数:
SqlConnectionStringBuilder
从这些值示例:
SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
sb.DataSource = Properties.Settings.Default.DBServerName;
sb.InitialCatalog = Properties.Settings.Default.DBDatabaseName;
sb.IntegratedSecurity = Properties.Settings.Default.DBUseIntegratedSecurity;
if (!sb.IntegratedSecurity)
{
sb.UserId = Properties.Settings.Default.DBUserName;
sb.Password = Properties.Settings.Default.DBPassword;
}
using (SqlConnection conn = new SqlConnect(sb.ConnectionString))
{
...
}
我会在Program.cs或App
类中生成一个静态属性,该类可以从SqlConnectionStringBuilder
返回连接字符串。
答案 1 :(得分:-1)
你的意思是你的数据库的connectionString?如果是这样,则应在数据库连接类中更改它。 如果您的数据库也存储在本地,则本地设备上的connectionString应该与普通路径类似。 像这样:
"C:\\Some_location\\database.extension"
扩展名取决于您使用的数据库类型,请将此字符串更改为数据库的位置。