我在C#中有项目,其中我有包含连接字符串构建器的类(名称:sql_conClass.cs):
public class myConnection
{
internal static class DataSource
{
private static string _ConnectionString;
public static string ConnectionString
{
get
{
if (_ConnectionString == null)
_ConnectionString = FunctionToDynamicallyCreateConnectionstring();
return _ConnectionString;
}
}
private static string FunctionToDynamicallyCreateConnectionstring()
{
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();
cb.DataSource = Properties.Settings.Default.sql;
cb.InitialCatalog = Properties.Settings.Default.database;
cb.UserID = Properties.Settings.Default.login;
cb.Password = Properties.Settings.Default.pass;
cb.ConnectTimeout = 120;
return cb.ToString();
}
}
现在,当我为ReportViewer创建dinamically DataSource时(我从服务器资源管理器中移出精确服务器,我用于ReportViewer的表)。该项目创建了自己与此服务器的连接。因此,当我创建项目的.exe文件时,每个人都会将此SqlConnection包含到此服务器,而无需更改它。
我的ConnectionBuilder存储在app.config中。我想问一下是否有办法将此Connectionbuilder的引用插入到app.config中?
答案 0 :(得分:0)
每个被动态移动到DataSet中的表都会创建自己的SqlConnection,它设置Integrated Security = True,例如: Data Source=SERVER\SQL;Initial Catalog=database;Integrated Security=True
。我发现在DataSet.Designer.cs中它创建了这行代码:
private void InitConnection() {
this._connection = new global::System.Data.SqlClient.SqlConnection();
this._connection.ConnectionString = global::tours.Properties.Settings.Default.toursConnectionString1;
}
我只需要提供对类的正确引用并将其更改为:
private void InitConnection() {
this._connection = new global::System.Data.SqlClient.SqlConnection();
this._connection.ConnectionString = myConnection.DataSource.ConnectionString;
}