21

C# 言語で 3 層プロジェクトを実行しているときに、Reader が閉じているときに無効な Read の呼び出しを試みています。私がやろうとしているのは、2 つのテーブルを結合してドロップダウン リストに表示することで住所データ列を取得することです。ここに私のデータアクセス層があります:

public List<Distribution> getDistributionAll()
{
    List<Distribution> distributionAll = new List<Distribution>();
    string address;
    SqlDataReader dr = FoodBankDB.executeReader("SELECT b.addressLineOne FROM dbo.Beneficiaries b INNER JOIN dbo.Distributions d ON d.beneficiary = b.id");

    while (dr.Read())
    {
        address = dr["addressLineOne"].ToString();
        distributionAll.Add(new Distribution(address));
    }

    return distributionAll;
}

そして、これは私の FoodBankDB クラスです:

public class FoodBankDB
{
    public static string connectionString = Properties.Settings.Default.connectionString;
    public static SqlDataReader executeReader(string query)
    {
        SqlDataReader result = null;
        System.Diagnostics.Debug.WriteLine("FoodBankDB executeReader: " + query);
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(query, connection);
        connection.Open();
        result = command.ExecuteReader();
        connection.Close();
        return result;
    }
}

これらを 2 つのクラスに分けて、接続文字列が変更されるたびに、FoodBankDB クラスを変更することでプロジェクト全体を簡単に修正できるようにしました。

これが私のビジネス ロジック レイヤーです。

public List<Distribution> getAllScheduledDistribution()
{
    List<Distribution> allDistribution = new List<Distribution>();
    Distribution distributionDAL = new Distribution();
    allDistribution = distributionDAL.getDistributionAll();
    return allDistribution;
}

最後になりましたが、私のプレゼンテーション レイヤーは次のとおりです。

List<Distribution> scheduledList = new List<Distribution>();
scheduledList = packBLL.getAllScheduledDistribution();
ddlScheduleList.DataSource = scheduledList;
ddlScheduleList.DataTextField = "address";
ddlScheduleList.DataValueField = "address";
ddlScheduleList.DataBind();

データ アクセス レイヤーと接続文字列クラスを分割しなければ、うまく機能していました。このエラーを解決する方法を知っている人はいますか?

前もって感謝します。

更新部分

public static string GetConnectionString()
{
    return connectionString;
}
4

2 に答える 2

34

リーダーを返す前に接続を閉じるため、機能しません。リーダーは、接続が開いている場合にのみ機能します。

result = command.ExecuteReader();
connection.Close();

return result; // here the reader is not valid

一般的に言えば、リーダーをビジネス層に戻すべきではありません。リーダーは、データ アクセス層でのみ使用する必要があります。それを使用してから、接続を閉じる必要があります。

むしろ、接続が閉じられた後に機能するオブジェクトを返す必要があります。たとえば、DataSetまたはDataTable代わりに DTO のコレクションです。例えば:

public List<Distribution> getDistributionAll()
{
    List<Distribution> distributionAll = new List<Distribution>();

    using (var connection = new SqlConnection(FoodBankDB.GetConnectionString())) // get your connection string from the other class here
    {
        SqlCommand command = new SqlCommand("SELECT b.addressLineOne FROM dbo.Beneficiaries b INNER JOIN dbo.Distributions d ON d.beneficiary = b.id", connection);
        connection.Open();
        using (var dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                string address = dr["addressLineOne"].ToString();

                distributionAll.Add(new Distribution(address));
            }
        }
    }

    return distributionAll;
}
于 2013-12-09T11:54:32.660 に答える