如何将方法插入SqlDataReader?

时间:2014-02-03 10:26:17

标签: c# methods sqldatareader

我正在尝试将方法传递给SqlDataReader,所以我有这个方法1获取数据列表,我有method2,当我运行时,它获取特定CustomerID购买的商品数量该程序我收到错误"Index was outside the bounds of the array."

 public List<Staff> method1()
{
    List<Staff> LBD = new List<Staff>();
    SqlConnection conn = new SqlConnection etc...
    SqlCommand command = new SqlCommand(SQL query goes here)

    conn.Open();
    SqlDatadata data = command.Executedata();
    while (data.Read())
    {
        Staff l = new Staff();
        l.IsStaff = data["IsStaff"].ToString();
        l.Name = data["Name"].ToString();
        l.Name = Convert.ToInt32(reader[this.method2(reader["ID"].ToString())]);

        ........
        LBD.Add(l);
    }
    conn.Close();
    return LBD;
}

        public int method2(string ID)
    {
        SqlConnection conn = new ......
        SqlCommand command = new SqlCommand("select COUNT (RowName) from TableName WHERE ID=@ID", conn);
        command.Parameters.AddWithValue("@RowName", ID);

        conn.Open();
        int x = command.ExecuteNonQuery();
        conn.Close();
        return x;
    }

2 个答案:

答案 0 :(得分:0)

此ID将更高或更低,或者此ID在读取器阵列中不可用

所以检查一下......

答案 1 :(得分:0)

确保使用与database中相同的列。我认为你使用的是错误的列名。