【发布时间】:2013-03-25 21:25:16
【问题描述】:
我的 C# 应用程序使用 DataSet 和 TableAdapters。它们是由 VS2008 GUI Tool 生成的。
例子:
右键项目->添加新项->数据集
此方法自动将连接字符串添加到app.config。
但这是连接字符串的硬线方法。我想以一种简单的方式更改连接字符串。但是当我使用数据集时,连接字符串会从应用程序属性中获取。 这种情况有什么解决办法吗?
这是我在 Settings.Designer.cs 文件中存储的连接字符串
namespace WindowsFormsApplication2.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=SLCERT\\SQLEMK;Initial Catalog=TestDataBase;Integrated Security=True")]
public string TestDataBaseConnectionString {
get {
return ((string)(this["TestDataBaseConnectionString"])); // this is the connection string get from the dataset's
}
}
}
}
app.config 包含
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="WindowsFormsApplication2.Properties.Settings.TestDataBaseConnectionString"
connectionString="Data Source=SLCERT\SQLEMK;Initial Catalog=TestDataBase;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
【问题讨论】:
-
什么应用做起来不容易?
-
还有什么比编辑 app.config 更容易的呢?
-
app.config 伴随您的应用程序。它的xml。可在任何文本编辑器中轻松编辑
标签: c# sql-server app-config