ASP.NET C# 数据访问层函数问题
我在我的 .NET 站点中使用 3 层架构。目前在我的 DAL(数据访问层)中,我有加载、更新、插入和删除,一切都工作正常。不过,我现在想创建一个函数来通过电子邮件地址获取数据。
下面是驻留在数据访问层中的 load
、insert
和 getByEmailAddress
public DataTable Load()
{
SqlConnection conn = new SqlConnection(MyGlobals.conString);
SqlDataAdapter dAd = new SqlDataAdapter("administratorGetAll", conn);
dAd.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet, "AdministratorsTable");
return dSet.Tables["AdministratorsTable"];
}
catch
{
throw;
}
finally
{
dSet.Dispose();
dAd.Dispose();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// Used to load records from database by email address
/// </summary>
public int GetByEmailAddress(AdministratorsBO administrator)
{
SqlConnection conn = new SqlConnection(MyGlobals.conString);
SqlCommand dCmd = new SqlCommand("administratorGetByEmailAddress", conn);
dCmd.CommandType = CommandType.StoredProcedure;
DataSet dSet = new DataSet();
try
{
dCmd.Parameters.AddWithValue("@emailAddress", administrator.EmailAddress);
SqlDataAdapter dAd = new SqlDataAdapter(dCmd);
dAd.Fill(dSet, "AdministratorsTable");
return Convert.ToInt32(dSet.Tables["AdministratorsTable"]);
}
catch
{
throw;
}
finally
{
dSet.Dispose();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// Used to insert records into database
/// </summary>
public int Insert(AdministratorsBO administrator)
{
SqlConnection conn = new SqlConnection(MyGlobals.conString);
conn.Open();
SqlCommand dCmd = new SqlCommand("administratorInsert", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@userName", administrator.UserName);
dCmd.Parameters.AddWithValue("@emailAddress", administrator.EmailAddress);
dCmd.Parameters.AddWithValue("@password", administrator.Password);
dCmd.Parameters.AddWithValue("@firstName", administrator.FirstName);
dCmd.Parameters.AddWithValue("@lastName", administrator.LastName);
dCmd.Parameters.AddWithValue("@isActive", administrator.IsActive);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
接下来是我的业务访问层:
public DataTable Load()
{
AdministratorsDAL aDAL = new AdministratorsDAL();
try
{
return aDAL.Load();
}
catch
{
throw;
}
finally
{
aDAL = null;
}
}
/// <summary>
/// Load records from database
/// </summary>
/// <returns></returns>
public DataTable GetByEmailAddress(AdministratorsBO administrator)
{
AdministratorsDAL aDAL = new AdministratorsDAL();
try
{
return aDAL.GetByEmailAddress(administrator);
}
catch
{
throw;
}
finally
{
aDAL = null;
}
}
/// <summary>
/// Insert records into database
/// </summary>
/// <param name="person"></param>
public int Insert(AdministratorsBO administrator)
{
AdministratorsDAL aDAL = new AdministratorsDAL();
try
{
return aDAL.Insert(administrator);
}
catch
{
throw;
}
finally
{
aDAL = null;
}
}
加载和插入非常标准和工作。但是 getByEmailAddress
未正确配置。我尝试混合加载和插入中的代码,但没有成功。任何人都可以给我一个提示、想法或可以解决此问题的东西,以便我可以使用 getByEmailAddress
顺便说一下,该函数应该命中一个存储过程,该过程通过发送的任何 emailAddress 输入来收集所有信息。
提前致谢!
I am using the 3-tier architecture in my .NET site. Currently in my DAL (Data Access Layer) I have Load, Update, Insert and Delete which is all working fine. However I now want to create a function to grab data by an email address.
Below is my load
, insert
and getByEmailAddress
that reside in my Data Access Layer
public DataTable Load()
{
SqlConnection conn = new SqlConnection(MyGlobals.conString);
SqlDataAdapter dAd = new SqlDataAdapter("administratorGetAll", conn);
dAd.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet, "AdministratorsTable");
return dSet.Tables["AdministratorsTable"];
}
catch
{
throw;
}
finally
{
dSet.Dispose();
dAd.Dispose();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// Used to load records from database by email address
/// </summary>
public int GetByEmailAddress(AdministratorsBO administrator)
{
SqlConnection conn = new SqlConnection(MyGlobals.conString);
SqlCommand dCmd = new SqlCommand("administratorGetByEmailAddress", conn);
dCmd.CommandType = CommandType.StoredProcedure;
DataSet dSet = new DataSet();
try
{
dCmd.Parameters.AddWithValue("@emailAddress", administrator.EmailAddress);
SqlDataAdapter dAd = new SqlDataAdapter(dCmd);
dAd.Fill(dSet, "AdministratorsTable");
return Convert.ToInt32(dSet.Tables["AdministratorsTable"]);
}
catch
{
throw;
}
finally
{
dSet.Dispose();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// Used to insert records into database
/// </summary>
public int Insert(AdministratorsBO administrator)
{
SqlConnection conn = new SqlConnection(MyGlobals.conString);
conn.Open();
SqlCommand dCmd = new SqlCommand("administratorInsert", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@userName", administrator.UserName);
dCmd.Parameters.AddWithValue("@emailAddress", administrator.EmailAddress);
dCmd.Parameters.AddWithValue("@password", administrator.Password);
dCmd.Parameters.AddWithValue("@firstName", administrator.FirstName);
dCmd.Parameters.AddWithValue("@lastName", administrator.LastName);
dCmd.Parameters.AddWithValue("@isActive", administrator.IsActive);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
Next is my Business Access Layer:
public DataTable Load()
{
AdministratorsDAL aDAL = new AdministratorsDAL();
try
{
return aDAL.Load();
}
catch
{
throw;
}
finally
{
aDAL = null;
}
}
/// <summary>
/// Load records from database
/// </summary>
/// <returns></returns>
public DataTable GetByEmailAddress(AdministratorsBO administrator)
{
AdministratorsDAL aDAL = new AdministratorsDAL();
try
{
return aDAL.GetByEmailAddress(administrator);
}
catch
{
throw;
}
finally
{
aDAL = null;
}
}
/// <summary>
/// Insert records into database
/// </summary>
/// <param name="person"></param>
public int Insert(AdministratorsBO administrator)
{
AdministratorsDAL aDAL = new AdministratorsDAL();
try
{
return aDAL.Insert(administrator);
}
catch
{
throw;
}
finally
{
aDAL = null;
}
}
The Load and Insert are pretty standard and work. However the getByEmailAddress
is not configured properly. I tried mixing code from the load and insert but I was unsuccessful. Can anyone give me a hint, idea or something that would fix this so I can use getByEmailAddress
By the way, that function is supposed to hit a stored procedure that gathers all information by whatever emailAddress input is sent through.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的业务层正在返回 GetByEmailAddress 的 DataTable,但您的数据访问层正在返回 GetByEmailAddress 的 int。你不想在你的 DAL:
中将返回更改为:
Your Business Layer is returning a DataTable for GetByEmailAddress but your Data Access Layer is returning an int for GetByEmailAddress. Don't you want in your DAL:
and the return changed to:
从您的业务层引用,您应该返回数据表而不是整数
Referring from you business layer you should be returning a datatable not an integer