2017-02-02 1 views
1

私は大学の割り当てのためにWindowsフォームソリューションを構築しようとしているときに問題に直面していました。Windowsフォームとデータベースの間で値を渡す

このソリューションはモバイルショップについてのものです。私はAppleとAndroidの2つのクラスを持っています。私は、データベーステーブルのデータを読み込み、エントリーをAndroidまたはAppleの電話機に分類し、フォームがロードされるときにすべての電話機をリストに表示する必要があります。

私は電話機を正常に分類できますが、エントリを読み取ろうとすると、2番目のエントリはまったく表示されませんが、常にフォームのリストに2回表示されます。

私は接続をしている間に大きなばかげたミスをしたことを知っていますが、私はそれを見つけることができません!

は、ここに私のコードです:

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) { 
    } 

Androidのクラスは同じですが、異なる他の機能とです。

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

第1質問。メソッド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つの電話機リストを作成しますOSの場合は、これらのリストの内容をフォームリストに入力してください – Wheels73

+0

私はそれを試してみるでしょう..ありがとうございます –

+0

また、カプセル化を見てください。データベース接続は、データを理想的にはビジネスクラスに戻す別のクラスになければなりません。ビジネスクラスは、電話機のリストを作成してフォームに戻します。 – Wheels73

0

、です。

SqlCommand dbCommand =新しいSqlCommand( "SelectAllPhones");各クラスの携帯電話のデータの両方のセットをドラッグして非常に少ないポイント(アンドロイド/ iphone)があるだけですので

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... 

    } 

    } 
} 

://ストアドプロシージャ

は次のようになります。必要なデータだけを取り戻すこともできます。

もちろん、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;あなたの既存のコードを使用して行を決定するより良い方法です。現時点では、データベース接続の数があなたの後ろのものではなく、あなたが必要としないように鉱山やホイールを使用する場合は冗長になります。処理する行数をあらかじめ知っておいてください。

+0

ありがとう、私は私の他のプロジェクトでこれを使用します –

関連する問題