将连接字符串从Web表单传递到业务逻辑组件 [英] Passing the connection string from a web form to a business logic component
问题描述
大家好..
我创建了一个从我的ASP.NET
webform中使用的业务逻辑组件。它可以工作,但是数据库的连接字符串是硬编码的,因为这个方法是
:
公共DataSet GetCategories()
{
SqlConnection conn = new SqlConnection(" Data Source = DEVSERVER; Initial
Catalog = XXXX; User ID = X; Password = Y");
SqlDataAdapter adapter = new SqlDataAdapter(" SELECT CAT_ID,CAT_NOM FROM
CATEGORIA ORDER BY CAT_NOM" conn);
DataSet ds = new DataSet() ;
adapter.Fill(ds);
返回ds;
}
我需要要知道传递web.config文件中存在的连接字符串的最佳方法是什么?
。我尝试在该业务中使用构造函数
类但没有成功。另外我已经读过ObjectDataSource使用
无状态组件所以我认为这里不允许使用属性。
问题是,唯一的解决方案我可以实现将
连接字符串作为参数传递给GetCategories?还有另一个吗?
由于此方法驻留在DLL中,我无法使用ConnectionManager类来访问web.config文件。
非常感谢
Jaime
如果你有它在Web.Config中: -
< add key =" ConnectionString" value =" server =(local); database = YourDB; integrated
security = true;" />
然后在你的app中执行: -
myconnection = New
SqlConnection(ConfigurationSettings.AppSettings(" C onnectionString"))
这就是你要找的。
Patrick
" Jaime Stuardo" < JS ****** @ manquehue.net>在消息中写道
新闻:ef ************** @ TK2MSFTNGP14.phx.gbl ...大家好..
我创建了一个从我的ASP.NET
webform中使用的业务逻辑组件。它工作正常,但是数据库的连接字符串是硬编码的,因为在这种方法中:
公共DataSet GetCategories()
{/> SqlConnection conn = new SqlConnection( Data Source = DEVSERVER; Initial
Catalog = XXXX; User ID = X; Password = Y");
SqlDataAdapter adapter = new SqlDataAdapter(" SELECT CAT_ID,CAT_NOM
FROM CATEGORIA ORDER BY CAT_NOM",conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
我需要知道传递web.config文件中存在的连接字符串的最佳方法是什么。我尝试在
商业类中使用构造函数,但没有成功。另外我已经读过ObjectDataSource使用
无状态组件所以我认为这里不允许使用属性
。
问题是,我能实现的唯一解决方案是通过
连接字符串作为GetCategories的参数?还有另外一个吗?
由于此方法驻留在DLL中,我无法使用ConnectionManager类
来访问web.config文件。
非常感谢
Jaime
最好的解决方案是编写自己的应用程序设置类(使用
接口)。这允许多次实现加载设置,但是
用于读取。
提示:使用工厂模型允许静态创建方法。
- 布鲁斯(sqlwork.com)
" Jaime Stuardo" < JS ****** @ manquehue.net>在消息中写道
新闻:ef ************** @ TK2MSFTNGP14.phx.gbl ...大家好..
我创建了一个从我的ASP.NET
webform中使用的业务逻辑组件。它工作正常,但是数据库的连接字符串是硬编码的,因为在这种方法中:
公共DataSet GetCategories()
{/> SqlConnection conn = new SqlConnection( Data Source = DEVSERVER; Initial
Catalog = XXXX; User ID = X; Password = Y");
SqlDataAdapter adapter = new SqlDataAdapter(" SELECT CAT_ID,CAT_NOM
FROM CATEGORIA ORDER BY CAT_NOM",conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
我需要知道传递web.config文件中存在的连接字符串的最佳方法是什么。我尝试在那个商业类中使用构造函数,但没有成功。另外我已经读过ObjectDataSource
使用无状态组件所以我认为不允许使用属性
问题是,我能实现的唯一解决方案是将
连接字符串作为参数传递给GetCategories?还有另一个吗?
由于此方法驻留在DLL中,我无法使用ConnectionManager类访问web.config文件。
非常感谢
Jaime
嗨帕特里克..
我不能这样做,因为这个方法是在一个被引用的DLL中
ASP.NET项目。当我从组件中使用AppSettings时,我收到一个
null。但是,如果我公开,我想我可以使用你的建议
DataSet GetCategories(String connectionStr)。
顺便说一句,我使用的是ASP.NET 2.0在此版本中
ConfigurationSettings.AppSettings报告为已过时。我应该使用
ConfigurationManager.AppSettings。
Jaime
" Patrick.O.Ige" < NA ******** @ hotmail.com>在消息中写道
news:em ************** @ TK2MSFTNGP12.phx.gbl ...如果你有它Web.Config如: -
< add key =" ConnectionString"
value =" server =(local); database = YourDB; integrated
security = true;" />
然后在你的app中做: -
myconnection =新的
SqlConnection(ConfigurationSettings.AppSettings(C onnectionString))
这就是你要找的东西。
Patrick
" Jaime Stuardo" < JS ****** @ manquehue.net>在消息中写道
新闻:ef ************** @ TK2MSFTNGP14.phx.gbl ...大家好..
<我已经创建了一个从我的ASP.NET
webform中使用的业务逻辑组件。它的工作原理,但是数据库的连接字符串是硬编码的,
在这种方法中:
公共DataSet GetCategories()
{/ / SqlConnection conn = new SqlConnection(" Data Source = DEVSERVER; Initial
Catalog = XXXX; User ID = X; Password = Y");
SqlDataAdapter adapter = new SqlDataAdapter(" SELECT CAT_ID,CAT_NOM
FROMCATEGORIA ORDER BY CAT_NOM",conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
返回ds;
}
我需要知道传递web.config文件中存在的连接字符串的最佳方法是什么。我尝试在
业务类中使用构造函数,但没有成功。另外我已经读过ObjectDataSource使用
无状态组件所以我认为不允许使用属性
这里。
问题是,我可以实现的唯一解决方案是将
连接字符串作为参数传递给GetCategories?还有另一个吗?
由于此方法驻留在DLL中,我无法使用ConnectionManager类
来访问web.config文件。
<非常感谢
Jaime
Hi all..
I have created a business logic component that is used from my ASP.NET
webform. It works, but connection string to the database is hard coded, as
in this method :
public DataSet GetCategories()
{
SqlConnection conn = new SqlConnection("Data Source=DEVSERVER;Initial
Catalog=XXXX;User ID=X;Password=Y");
SqlDataAdapter adapter = new SqlDataAdapter("SELECT CAT_ID, CAT_NOM FROM
CATEGORIA ORDER BY CAT_NOM", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
I need to know what is the best way to pass the connection string that is
present in the web.config file. I tried using a constructor in that business
class but with no success. Also I have read that ObjectDataSource uses
stateless components so I think the usage of properties isn''t allowed here.
The question is, the only solution I could implement is passing the
connection string as a parameter to GetCategories? is there another one?
Since this method resides in a DLL, I cannot use ConnectionManager class to
access web.config file.
Thanks a lot
Jaime
if you have it in Web.Config like:-
<add key="ConnectionString" value="server=(local);database=YourDB;integrated
security=true;" />
Then in you app do:-
myconnection = New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
Is that what you are looking for.
Patrick
"Jaime Stuardo" <js******@manquehue.net> wrote in message
news:ef**************@TK2MSFTNGP14.phx.gbl...Hi all..
I have created a business logic component that is used from my ASP.NET
webform. It works, but connection string to the database is hard coded, as
in this method :
public DataSet GetCategories()
{
SqlConnection conn = new SqlConnection("Data Source=DEVSERVER;Initial
Catalog=XXXX;User ID=X;Password=Y");
SqlDataAdapter adapter = new SqlDataAdapter("SELECT CAT_ID, CAT_NOM FROM CATEGORIA ORDER BY CAT_NOM", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
I need to know what is the best way to pass the connection string that is
present in the web.config file. I tried using a constructor in that business class but with no success. Also I have read that ObjectDataSource uses
stateless components so I think the usage of properties isn''t allowed here.
The question is, the only solution I could implement is passing the
connection string as a parameter to GetCategories? is there another one?
Since this method resides in a DLL, I cannot use ConnectionManager class to access web.config file.
Thanks a lot
Jaime
the best solution is to write you own application setting class (use
interfaces). this allows multiple implementation of loading settings, but
one for reading.
hint: use the factory model to allow a static create method.
-- bruce (sqlwork.com)
"Jaime Stuardo" <js******@manquehue.net> wrote in message
news:ef**************@TK2MSFTNGP14.phx.gbl...Hi all..
I have created a business logic component that is used from my ASP.NET
webform. It works, but connection string to the database is hard coded, as
in this method :
public DataSet GetCategories()
{
SqlConnection conn = new SqlConnection("Data Source=DEVSERVER;Initial
Catalog=XXXX;User ID=X;Password=Y");
SqlDataAdapter adapter = new SqlDataAdapter("SELECT CAT_ID, CAT_NOM
FROM CATEGORIA ORDER BY CAT_NOM", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
I need to know what is the best way to pass the connection string that is
present in the web.config file. I tried using a constructor in that
business class but with no success. Also I have read that ObjectDataSource
uses stateless components so I think the usage of properties isn''t allowed
here.
The question is, the only solution I could implement is passing the
connection string as a parameter to GetCategories? is there another one?
Since this method resides in a DLL, I cannot use ConnectionManager class
to access web.config file.
Thanks a lot
Jaime
Hi Patrick..
I cannot do that since this method is in a DLL that is referenced by the
ASP.NET project. When I use AppSettings from the component, I receive a
null. However I think I could use what you suggested if I have public
DataSet GetCategories(String connectionStr).
By the way, I use ASP.NET 2.0 and in this version
ConfigurationSettings.AppSettings is reported as obsolete. I should use
ConfigurationManager.AppSettings instead.
Jaime
"Patrick.O.Ige" <na********@hotmail.com> wrote in message
news:em**************@TK2MSFTNGP12.phx.gbl...if you have it in Web.Config like:-
<add key="ConnectionString"
value="server=(local);database=YourDB;integrated
security=true;" />
Then in you app do:-
myconnection = New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
Is that what you are looking for.
Patrick
"Jaime Stuardo" <js******@manquehue.net> wrote in message
news:ef**************@TK2MSFTNGP14.phx.gbl...Hi all..
I have created a business logic component that is used from my ASP.NET
webform. It works, but connection string to the database is hard coded,
as
in this method :
public DataSet GetCategories()
{
SqlConnection conn = new SqlConnection("Data Source=DEVSERVER;Initial
Catalog=XXXX;User ID=X;Password=Y");
SqlDataAdapter adapter = new SqlDataAdapter("SELECT CAT_ID, CAT_NOM
FROMCATEGORIA ORDER BY CAT_NOM", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
I need to know what is the best way to pass the connection string that is
present in the web.config file. I tried using a constructor in that
businessclass but with no success. Also I have read that ObjectDataSource uses
stateless components so I think the usage of properties isn''t allowed
here.
The question is, the only solution I could implement is passing the
connection string as a parameter to GetCategories? is there another one?
Since this method resides in a DLL, I cannot use ConnectionManager class
toaccess web.config file.
Thanks a lot
Jaime
这篇关于将连接字符串从Web表单传递到业务逻辑组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!