2017-02-02 82 views
1

在爲大學任務構建Windows窗體解決方案時遇到問題,並希望有人能指出我的錯誤。在Windows窗體和數據庫之間傳遞值

解決方案是關於移動商店。我有兩個類,蘋果和Android的形式。我需要讀取數據庫表中的數據,將條目分類爲Android或Apple電話,然後在表單加載時將所有電話顯示在列表中。

我可以成功地對手機進行分類,但是在嘗試閱讀條目時,我總是在表單上列出兩次相同的條目,而第二條條目完全沒有出現。

我知道我在做連接時犯了一個很大的錯誤,但是我找不到它!

這裏是我的代碼:

public abstract class MobilePhone { 
    private Int32 phoneID; 
    private string operatingSystem; 
    private string make; 
    private string model; 
    public enum Condition { Poor, Fair, Good, Mint }; 
    private Condition condition; 
    private decimal originalPrice; 
    private DateTime datePurchase; 
    private string description; 
    private clsDataConnection dbConnection; 

    //constructor 
    public MobilePhone(string make, string model, decimal originalPrice, DateTime datePurchase, Condition condition, string description) { 
     this.make = make; 
     this.model = model; 
     this.originalPrice = originalPrice; 
     this.datePurchase = datePurchase; 
     this.condition = condition; 
     this.description = description; 
    } 

沒有完成,但是這就是相關:

public class ApplePhone : MobilePhone { 
    decimal ApproxValue; 
    public ApplePhone(string make, string model, decimal originalPrice, DateTime datePurchase, Condition condition, string description) 
     : base(make, model, originalPrice, datePurchase, condition, description) { 
    } 

安卓類是相同的,但不同的其他功能。

class Shop { 
    clsDataConnection dbConnection; 
    const int NotAdded = -1; // invalid primary key 
    private string name; 
    private decimal ApproxValue; 
    private Int32 phoneID; 
    private string operatingSystem; 
    private string make; 
    private string model; 
    private MobilePhone.Condition condition; 
    private decimal originalPrice; 
    private DateTime datePurchase; 
    private string description; 
    Int32 Index; 
    private List<MobilePhone> phonesForSale; 

    //constructor 
    public Shop(string name) { 
     this.name = name; 
    } 

    MobilePhone phone; 

    public void SelectAll() { 
     dbConnection = new clsDataConnection(); 
     dbConnection.Execute("SellectAllPhones"); 
    } 

    public void FilterByOperatingSystem(string operatingSystem) { 
     dbConnection = new clsDataConnection(); 
     dbConnection.AddParameter("@OperatingSystem", operatingSystem); 
     dbConnection.Execute("FilterByOperatingSystem"); 
    } 

    public Int32 Count { 
     get { 
      //return the count of records 
      return dbConnection.Count; 
     } 
    } 

    public string DescribeCurrentPhone(int Index) { 
     Int32 phoneID; 
     string make; 
     string model; 
     MobilePhone.Condition condition; 
     decimal originalPrice; 
     DateTime datePurchase; 
     string description; 

     phoneID = Convert.ToInt32(phonesForSale[Index].PhoneID); 
     make = Convert.ToString(phonesForSale[Index].Make); 
     model = Convert.ToString(phonesForSale[Index].Model); 
     condition = phonesForSale[Index].GetCondition; 
     originalPrice = Convert.ToDecimal(phonesForSale[Index].OriginalPrice); 
     datePurchase = Convert.ToDateTime(phonesForSale[Index].DatePurchased); 
     description = Convert.ToString(phonesForSale[Index].Description); 
     //set up a new object of class list item    
     string listItemText = make + " " + "|" + " " + model + " " + "|" + " " + condition + " " + "|" + " " + "£" + Math.Round(originalPrice, 2) + " " + "|" + " " + datePurchase.ToShortDateString() + " " + "|" + " " + description; 
     return listItemText; 
    } 

    public List<MobilePhone> Allphones { 
     get { 
      phonesForSale = new List<MobilePhone>(); 
      int count = Count; 
      Index = 0; 
      while (Index < count) { 
       phoneID = Convert.ToInt32(dbConnection.DataTable.Rows[Index]["PhoneId"]); 
       operatingSystem = Convert.ToString(dbConnection.DataTable.Rows[Index]["OperatingSystem"]); 
       make = Convert.ToString(dbConnection.DataTable.Rows[Index]["Make"]); 
       model = Convert.ToString(dbConnection.DataTable.Rows[Index]["Model"]); 
       string conditionString = Convert.ToString(dbConnection.DataTable.Rows[Index]["Condition"]); 
       originalPrice = Convert.ToInt32(dbConnection.DataTable.Rows[Index]["OriginalPrice"]); 
       datePurchase = Convert.ToDateTime(dbConnection.DataTable.Rows[Index]["DatePurchased"]); 
       description = Convert.ToString(dbConnection.DataTable.Rows[Index]["Description"]); 
       // Set Condition 
       if (conditionString == "Poor") { 
        condition = MobilePhone.Condition.Poor; 
       } else if (conditionString == "Fair") { 
        condition = MobilePhone.Condition.Fair; 
       } else if (conditionString == "Good") { 
        condition = MobilePhone.Condition.Good; 
       } else if (conditionString == "Mint") { 
        condition = MobilePhone.Condition.Mint; 
       } 
       //check Operating System 
       if (operatingSystem == "IOS") { 
        phone = new ApplePhone(make, model, originalPrice, datePurchase, condition, description); 
        //ApproxValue = ApplePhone.CalculateApproximateValue(); 
       } else if (operatingSystem == "Android") { 
        phone = new AndroidPhone(make, model, originalPrice, datePurchase, condition, description); 
        //ApproxValue = AndroidPhone.CalculateApproximateValue(); 
       } 
       Index++; 
       phonesForSale.Add(phone); 
      } 
      return phonesForSale; 
     } 
    } 

和表單代碼:

public partial class FormMain : Form { 
    public FormMain() { 
     InitializeComponent(); 
     Shop shop = new Shop(""); 
    } 

    private void FormMain_Load(object sender, EventArgs e) { 
     DisplayItems(""); 
    } 

    protected int DisplayItems(string operatingSystem) { 
     Shop MyShop = new Shop(""); 
     Int32 RecordCount; 
     Int32 Index = 0; 
     Int32 PID; 

     if (operatingSystem != "") { 
      MyShop.FilterByOperatingSystem(operatingSystem); 
     } else { 
      MyShop.SelectAll(); 
     } 

     RecordCount = MyShop.Count; 
     ArrayList MyPhones = new ArrayList(); 
     while (Index < RecordCount) { 
      // I Suspect this line is the problem but don't know how to fix it 
      PID = MyShop.Allphones[Index].PhoneID 
      string listItemText = MyShop.DescribeCurrentPhone(PID); 
      //add the new item to the list 
      MyPhones.Add(listItemText); 
      //increment the index 
      Index++; 
     } 
     listBox1.DataSource = MyPhones; 
     return RecordCount; 
    } 

我不是用來連接到數據庫,所以任何建議將是幫助!

+0

第一個問題。方法SelectAll()。這是幹什麼的?它似乎沒有返回任何數據,它只是調用dbConnection.Execute(「SellectAllPhones」) – Wheels73

+0

它執行此存儲過程,以便我可以讀取它的結果數據(表) –

回答

0

替代您所做的DB連接的下面是一個例子

List<MyPhone> myIPhoneList = new List<Myphone>(); 
    List<MyPhone> myAndroidList = new List<Myphone>(); 

SqlConnection myDBConnection = new SqlConnection("MyConnectionString"); //DB Connection 
      SqlCommand dbCommand = new SqlCommand("SelectAllPhones"); //Stored Procedure 
      SqlDataReader recordReader = dbCommand.ExecuteReader(); //Execute 

      //Read records return in to phone objects 
      while (recordReader.Read()) { 
       var phoneField1 = recordReader["PhoneField1FromDatabase"]; 
       var phoneField2 = recordReader["PhoneField2FromDatabase"]; 
       //etc... 

       var myPhone = new MyPhone(); 
       myPhone.Name = phoneField1; 
       //etc... 
       if (myPhone.OS == "iPhone") 
       myIPhoneList.Add(myPhone); 

       if (myPhone.OS = "Android") 
       myAndroidList.Add(myPhone); 
      } 
+0

創建自己2個電話列表,每個1個操作系統,然後填入這些列表的內容到您的表格列表 – Wheels73

+0

我會試試看,謝謝 –

+0

也看封裝。您的數據庫連接應該位於一個單獨的類中,該類將數據返回給理想的業務類,然後創建這些業務類並將這些列表返回給您的表單。 – Wheels73

0

只是扭到車輪迴答說真的,

我會親自把過濾器上存儲的進程內。

SqlCommand dbCommand = new SqlCommand(「SelectAllPhones」); //存儲過程

變成類似:

using (SqlConnection conn = new SqlConnection()) 
{ 
    using (SqlCommand cmd = new SqlCommand("SelectAllPhones", conn)) 
    { 
    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@OS", SqlDbType = SqlDbType.VarChar, Direction = ParameterDirection.Input, Value = phoneOS }); 
    cmd.CommandType = CommandType.StoredProcedure; 

    SqlDataReader reader = cmd.ExecuteReader(); 

    while (reader.Read()) 
    { 
     // load your data... 

    } 

    } 
} 

只是因爲很少有一點爲每個類拖動兩組電話資料(Android/iPhone)的。你也可以只回收你需要的數據。

當然,Stored-Proc將需要更新以迎合參數。

類似: 和PhoneOS = @OS

需要追加到您的SQL條件。

clsDataConnection dbConnection;我是不知道的 - 這是第三方圖書館還是你寫的並沒有包括在內的課程?

public Int32 Count 
{ 
    get 
    { 
     //return the count of records 
     return dbConnection.Count; 
    } 
} 

dbConnection.Count看起來非常不標準。不讀取就好像你想獲得行數,更多的連接數 - 這在這裏是無效的。

dbConnection.DataTables [0]。Rows.Count;將是使用現有代碼確定行的更好方法,因爲目前它讀起來好像您計算的數據庫連接的數量不是您之後的數量 - 並且如果使用我的或Wheels,將會是多餘的,因爲您不需要事先知道您要處理的行數。

+0

謝謝,我會在我的其他項目上使用這個 –