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;
}