public ArrayList ChequeACpayee(string Payee, DateTime Date)
{
ArrayList arr = new ArrayList();
try
{
con.Open();
SqlCommand cmd = new SqlCommand("ViewChequeforAcPayee", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p = new SqlParameter();
p = cmd.Parameters.AddWithValue("@PayeeName", Payee);
p = cmd.Parameters.AddWithValue("@Date", Date);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ChequeInfo infoCheque = new ChequeInfo();
infoCheque.AcPayee = dr["AcPayee"].ToString();
arr.Add(infoCheque);
}
dr.Close();
con.Close();
}
catch (Exception e)
{
}
return arr;
}
ArrayList ArrAcpayee = ChequeAcPayee.ChequeACpayee (DDLPayee.SelectedItem.ToString(), BDPSelectDate.SelectedDate);
Run Code Online (Sandbox Code Playgroud)
如何从这个arraylist中获取单个值?该值必须转换为字符串.
您可以使用索引器和强制转换:
string payee = (string) ArrAcpayee[0];
Run Code Online (Sandbox Code Playgroud)
但你应该先检查尺寸:
if (ArrAcpayee.Length != 1)
{
// Create your own more appropriate exception
throw new SomethingWentWrongException("Expected 1 payee entry; got "
+ ArrAcpayee.Length);
}
string payee = (string) ArrAcpayee[0];
Run Code Online (Sandbox Code Playgroud)
但是,我关注代码的其他方面:
List<T>?ChequeACpayee方法中会有多个结果?如果没有,那么该方法应返回一个字符串,如果没有一个结果则抛出using语句来关闭数据库连接和数据读取器,而不是Close()手动调用.否则,如果抛出异常,您将泄漏连接.| 归档时间: |
|
| 查看次数: |
3728 次 |
| 最近记录: |