ASP.NET C# 数据访问层函数问题

发布于 2024-10-25 03:06:09 字数 4311 浏览 1 评论 0原文

我在我的 .NET 站点中使用 3 层架构。目前在我的 DAL(数据访问层)中,我有加载、更新、插入和删除,一切都工作正常。不过,我现在想创建一个函数来通过电子邮件地址获取数据。

下面是驻留在数据访问层中的 loadinsertgetByEmailAddress

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦冥 2024-11-01 03:06:09

您的业​​务层正在返回 GetByEmailAddress 的 DataTable,但您的数据访问层正在返回 GetByEmailAddress 的 int。你不想在你的 DAL:

public DataTable GetByEmailAddress(AdministratorsBO administrator)

中将返回更改为:

return dSet.Tables["AdministratorsTable"];

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:

public DataTable GetByEmailAddress(AdministratorsBO administrator)

and the return changed to:

return dSet.Tables["AdministratorsTable"];
自由范儿 2024-11-01 03:06:09

从您的业务层引用,您应该返回数据表而不是整数

public DataTable GetByEmailAddress(AdministratorsBO administrator)
{
    Using (SqlConnection conn = new SqlConnection(MyGlobals.conString))
    {
        SqlCommand dCmd = new SqlCommand("administratorGetByEmailAddress", conn);
        dCmd.CommandType = CommandType.StoredProcedure;
        DataSet dSet = new DataSet();

        dCmd.Parameters.AddWithValue("@emailAddress", administrator.EmailAddress);

        SqlDataAdapter dAd = new SqlDataAdapter(dCmd);

        dAd.Fill(dSet);
        return dSet.Tables[0];
    }
}

Referring from you business layer you should be returning a datatable not an integer

public DataTable GetByEmailAddress(AdministratorsBO administrator)
{
    Using (SqlConnection conn = new SqlConnection(MyGlobals.conString))
    {
        SqlCommand dCmd = new SqlCommand("administratorGetByEmailAddress", conn);
        dCmd.CommandType = CommandType.StoredProcedure;
        DataSet dSet = new DataSet();

        dCmd.Parameters.AddWithValue("@emailAddress", administrator.EmailAddress);

        SqlDataAdapter dAd = new SqlDataAdapter(dCmd);

        dAd.Fill(dSet);
        return dSet.Tables[0];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文