【发布时间】:2015-06-17 02:26:20
【问题描述】:
1 解决方案。
2个项目(1.WEB-2.三层结构)
调用存储过程: Web -> BOL -> BLL -> DAL。
DAL的方法:获取连接字符串并执行。
public DataSet ExecuteSQL(string sp, Persona user)
{
EH eh = new EH(); // Error_Handling.cs object
try
{
DataSet ds = new DataSet();
string connectionString = null;
connectionString = GetNewConnection("BO"); //HERE COMES THE ERROR
SqlConnection conn = new SqlConnection(connectionString);
using (conn)
{ ....
GetNewConnection()。
public string GetNewConnection(string server)
{
return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;
}
App.Config片段命名所需的连接
<connectionStrings>
<add name="BO"
connectionString="Data Source=Server;Initial Catalog=BO;User ID=WebUser;Password=333;Integrated Security=False"
providerName="System.Data.SqlClient" />
从 GetNewConnection() 返回时出现以下异常
- $exception {"对象引用未设置为对象的实例。"} System.Exception {System.NullReferenceException}
为什么它得到一个空引用?
【问题讨论】:
-
App.config 用于 web 项目?不应该是 web.config 吗?
-
顺便说一句:在
GetNewConnection,你不应该使用server参数而不是硬编码"BO"吗? -
@AndreCalil 是的,这是为了测试目的。
-
DLL 通常没有自己的配置文件(配置系统当然没有使这种情况变得容易的默认设置)。这是因为 DLL 被加载到可执行进程中 - 配置系统假定所有配置都将成为该可执行文件的一部分。