db没有填充通用列表

时间:2012-06-05 05:17:33

标签: c# asp.net sql

我正在尝试填充一个通用集合,但遇到了问题。我正在尝试填充myBookings,它应该存储List。下面的方法应该使用正确的List填充myBookings,但由于某种原因,当我计算结果(int c)时,我得到0的回报。任何人都可以看到我做错了吗?

// .cs

public partial class _Default : System.Web.UI.Page
{    
    iClean.Bookings myBookings = new iClean.Bookings();
    iClean.Booking myBooking = new iClean.Booking();
    iClean.Controller myController = new iClean.Controller();

    ListItem li = new ListItem();

    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentFname.Text = Profile.FirstName;
        CurrentUname.Text = Profile.UserName;
        CurrentLname.Text = Profile.LastName;

        myBookings.AllBookings = this.GetBookings();

        int c = myBookings.AllBookings.Count();

        Name.Text = c.ToString();
        Address.Text = myBooking.Address;
        Phone.Text = myBooking.Phone;
        Date.Text = myBooking.DueDate.ToString();
        Comments.Text = myBooking.Comments;        
    }

    public List<iClean.Booking> GetBookings()
    {
        List<iClean.Booking> bookings = new List<iClean.Booking>();
        ArrayList records = this.Select("Bookings", "");
        for (int i = 0; i < records.Count; i++)
        {
            iClean.Booking tempBooking = new iClean.Booking();
            Hashtable row = (Hashtable)records[i];
            tempBooking.ID = Convert.ToInt32(row["ID"]);
            tempBooking.Name = Convert.ToString(row["ClientName"]);
            tempBooking.Address = Convert.ToString(row["ClientAddress"]);
            tempBooking.Phone = Convert.ToString(row["ClientPhone"]);

            tempBooking.DueDate = Convert.ToDateTime(row["Bookingdate"]);
            tempBooking.Completed = Convert.ToBoolean(row["Completed"]);
            tempBooking.Paid = Convert.ToBoolean(row["Paid"]);
            tempBooking.Cancelled = Convert.ToBoolean(row["Cancelled"]);
            tempBooking.ReasonCancelled = Convert.ToString(row["ReasonCancelled"]);
            tempBooking.ContractorPaid = Convert.ToBoolean(row["ContractorPaid"]);
            tempBooking.Comments = Convert.ToString(row["Comments"]);

            tempBooking.Windows = Convert.ToBoolean(row["Windows"]);
            tempBooking.Gardening = Convert.ToBoolean(row["Gardening"]);
            tempBooking.IndoorCleaning = Convert.ToBoolean(row["IndoorCleaning"]);

            bookings.Add(tempBooking);
        }

        return bookings;
    }

    public ArrayList Select(string table, string conditions)
    {
        // Create something to hosue the records.
        ArrayList records = new ArrayList();

        try
        {
            // Open a connection.
            OleDbConnection myConnection = new OleDbConnection(this.getConnectionString());
            myConnection.Open();

            // Generate the SQL
            string sql = "SELECT * FROM " + table;
            if (conditions != "") { sql += " WHERE " + conditions; }

            // Console.WriteLine("Select SQL: " + sql); // In case we need to debug

            // Run the SQL
            OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
            OleDbDataReader myReader = myCommand.ExecuteReader();

            // Go through the rows that were returned ...
            while (myReader.Read())
            {
                // ... create Hashtable to keep the columns in, ...
                Hashtable row = new Hashtable();

                // ... add the fields ...
                for (int i = 0; i < myReader.FieldCount; i++)
                {
                    row.Add(myReader.GetName(i), myReader[i]);
                }

                // ... and store the row.
                records.Add(row);
            }

            // Make sure to close the connection
            myConnection.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        return records;
    }

    public string getConnectionString()
    {
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=IQQuotes.accdb;";

        return connectionString;
    }
}

1 个答案:

答案 0 :(得分:0)

只是一个猜测,但试试这个:

public List<iClean.Booking> GetBookings()
{
    List<iClean.Booking> bookings = new List<iClean.Booking>();
    ArrayList records = this.Select("Bookings", "");
    iClean.Booking tempBooking = new iClean.Booking();
    for (int i = 0; i < records.Count; i++)
    {
        tempBooking = new iClean.Booking();
        Hashtable row = (Hashtable)records[i];
        tempBooking.ID = Convert.ToInt32(row["ID"]);
        tempBooking.Name = Convert.ToString(row["ClientName"]);
        tempBooking.Address = Convert.ToString(row["ClientAddress"]);
        tempBooking.Phone = Convert.ToString(row["ClientPhone"]);

        tempBooking.DueDate = Convert.ToDateTime(row["Bookingdate"]);
        tempBooking.Completed = Convert.ToBoolean(row["Completed"]);
        tempBooking.Paid = Convert.ToBoolean(row["Paid"]);
        tempBooking.Cancelled = Convert.ToBoolean(row["Cancelled"]);
        tempBooking.ReasonCancelled = Convert.ToString(row["ReasonCancelled"]);
        tempBooking.ContractorPaid = Convert.ToBoolean(row["ContractorPaid"]);
        tempBooking.Comments = Convert.ToString(row["Comments"]);

        tempBooking.Windows = Convert.ToBoolean(row["Windows"]);
        tempBooking.Gardening = Convert.ToBoolean(row["Gardening"]);
        tempBooking.IndoorCleaning = Convert.ToBoolean(row["IndoorCleaning"]);

        bookings.Add(tempBooking);
    }

    return bookings;
}