2012-04-18 164 views
0

這可能是一個愚蠢的問題,如果是的話我表示歉意。我有一個程序,用戶在其中輸入數據到一個窗體中並點擊一個按鈕。該按鈕將輸入的數據保存到MS Access 2010數據庫中。從數據庫程序中訪問保存的數據

我的問題是這樣的:點擊按鈕並保存數據後,如果我從Access打開數據庫,我應該能看到保存的數據嗎?當我運行該程序時,我沒有收到任何錯誤消息,並且一切似乎都正常,但是當我從Access打開表格時,它是空的。這是因爲從Access打開數據庫會打開一個不同的實例還是數據只是不被保存?

下面是從Form類代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace P90XProgram 
{ 
    public partial class AbRipperXForm : Form 
    { 
     private AbRipperXBOL busObject = 
      new AbRipperXBOL();   

     //default constructor 
     public AbRipperXForm() 
     { 
      InitializeComponent(); 
      busObject.InitializeConnection(); 
     } 

     //event handler for data input 
     private void btnEnterAbRipperXInfo_Click(object sender, EventArgs e) 
     { 
      //convert input data to int datatype and assign to properties 
      busObject.InAndOuts = int.Parse(this.txtInAndOuts.Text); 
      busObject.ForwardBicycles = int.Parse(
       this.txtForwardBicycles.Text); 
      busObject.ReverseBicycles = int.Parse(
       this.txtReverseBicycles.Text); 
      busObject.CrunchyFrog = int.Parse(this.txtCrunchyFrog.Text); 
      busObject.CrossLegWideLegSitups = int.Parse(
       this.txtCrossLegWideLegSitups.Text); 
      busObject.FiferScissors = int.Parse(this.txtFiferScissors.Text); 
      busObject.HipRockNRaise = int.Parse(this.txtHipRockNRaise.Text); 
      busObject.PulseUpsHeelsToHeaven = int.Parse(
       this.txtPulseUpsHeelsToHeaven.Text); 
      busObject.VUpRollUpCombos = int.Parse(this.txtVUpRollUpCombos.Text); 
      busObject.ObliqueVUps = int.Parse(this.txtObliqueVUps.Text); 
      busObject.LegClimbs = int.Parse(this.txtLegClimbs.Text); 
      busObject.MasonTwists = int.Parse(this.txtMasonTwists.Text); 

      //call method to save input data 
      busObject.SaveData(); 

      //clear text boxes of data 
      this.txtInAndOuts.Clear(); 
      this.txtForwardBicycles.Clear(); 
      this.txtReverseBicycles.Clear(); 
      this.txtCrunchyFrog.Clear(); 
      this.txtCrossLegWideLegSitups.Clear(); 
      this.txtFiferScissors.Clear(); 
      this.txtHipRockNRaise.Clear(); 
      this.txtPulseUpsHeelsToHeaven.Clear(); 
      this.txtVUpRollUpCombos.Clear(); 
      this.txtObliqueVUps.Clear(); 
      this.txtLegClimbs.Clear(); 
      this.txtMasonTwists.Clear(); 
    } 

這是我的業務對象層代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 
using System.Configuration; 

namespace P90XProgram 
{ 
    public class AbRipperXBOL 
    { 
     int inAndOuts = 0, 
      forwardBicycles = 0, 
      reverseBicycles = 0, 
      crunchyFrog = 0, 
      crossLegWideLegSitups = 0, 
      fiferScissors = 0, 
      hipRockNRaise = 0, 
      pulseUpsHeelsToHeaven = 0, 
      vUpRollUpCombos = 0, 
      obliqueVUps = 0, 
      legClimbs = 0, 
      masonTwists = 0; 

     OleDbConnection aConnection = 
      new OleDbConnection(
       "Provider=Microsoft.ACE.OLEDB.12.0;" + 
       "Data Source=P90XDatabase.accdb;");  

     public AbRipperXBOL() 
     {    
     } 

     //property for inAndOuts variable 
     public int InAndOuts 
     { 
      get { return inAndOuts; } 
      set { inAndOuts = value; } 
     } 

     //property for forwardBicycles variable 
     public int ForwardBicycles 
     { 
      get { return forwardBicycles; } 
      set { forwardBicycles = value; } 
     } 

     //property for reverseBicycles variable 
     public int ReverseBicycles 
     { 
      get { return reverseBicycles; } 
      set { reverseBicycles = value; } 
     } 

     //property for crunchyFrog variable 
     public int CrunchyFrog 
     { 
      get { return crunchyFrog; } 
      set { crunchyFrog = value; } 
     } 

     //property for crossLegWideLegSitups variable 
     public int CrossLegWideLegSitups 
     { 
      get { return crossLegWideLegSitups; } 
      set { crossLegWideLegSitups = value; } 
     } 

     //property for fiferScissors variable 
     public int FiferScissors 
     { 
      get { return fiferScissors; } 
      set { fiferScissors = value; } 
     } 

     //property for hipRockNRaise variable 
     public int HipRockNRaise 
     { 
      get { return hipRockNRaise; } 
      set { hipRockNRaise = value; } 
     } 

     //property for pulseUpsHeelsToHeaven 
     public int PulseUpsHeelsToHeaven 
     { 
      get { return pulseUpsHeelsToHeaven; } 
      set { pulseUpsHeelsToHeaven = value; } 
     } 

     //property for vUpRollUpCombos variable 
     public int VUpRollUpCombos 
     { 
      get { return vUpRollUpCombos; } 
      set { vUpRollUpCombos = value; } 
     } 

     //property for obliqueVUps variable 
     public int ObliqueVUps 
     { 
      get { return obliqueVUps; } 
      set { obliqueVUps = value; } 
     } 

     //property for legClimbs variable 
     public int LegClimbs 
     { 
      get { return legClimbs; } 
      set { legClimbs = value; } 
     } 

     //property for masonTwists variable 
     public int MasonTwists 
     { 
      get { return masonTwists; } 
      set { masonTwists = value; } 
     } 

     public void InitializeConnection() 
     { 
      AbRipperXDAL.InitializeConnection(aConnection); 
     } 

     public void SaveData() 
     { 
      AbRipperXDAL.SaveData(this); 
     }   

     public static void BackToMainSchedule() 
     { 
      P90xScheduleForm f1;    

      if (Application.OpenForms["P90xScheduleForm"] == null) 
      { 
       f1 = new P90xScheduleForm(); 
       f1.Name = "P90xScheduleForm"; 
      } 
      else 
      { 
       f1 = Application.OpenForms["P90xScheduleForm"] as P90xScheduleForm; 
      } 

      f1.Show(); 
     } 
    } 
} 

這是從我的數據訪問層的代碼:

using System; 
using System.Collections.Generic; 
using System.Collections; 
using System.Text; 
using System.Data; 
using System.Data.OleDb; 

namespace P90XProgram 
{ 
    class AbRipperXDAL 
    { 
     static OleDbConnection aConnection = null; 

     public static void InitializeConnection(OleDbConnection aDbConnection) 
     { 
      aConnection = aDbConnection; 
      aConnection.Open(); 
     } 

     public static void SaveData(AbRipperXBOL busObject) 
     { 
      try 
      { 
       String sSQLCommand = "INSERT INTO AbRipperX (InAndOuts, " + 
        "ForwardBicycles, ReverseBicycles, CrunchyFrog, " + 
        "CrossLegWideLegSitups, Fiferscissors, HipRockNRaise, " + 
        "PulseUpsHeelsToHeaven, VUpRollUpCombos, ObliqueVUps, " + 
        "LegClimbs, MasonTwists) VALUES ('" + busObject.InAndOuts + 
        "','" + busObject.ForwardBicycles + "','" + 
        busObject.ReverseBicycles + "','" + busObject.CrunchyFrog + 
        "','" + busObject.CrossLegWideLegSitups + "','" + 
        busObject.FiferScissors + "','" + busObject.HipRockNRaise + 
        "','" + busObject.PulseUpsHeelsToHeaven + "','" + 
        busObject.VUpRollUpCombos + "','" + busObject.ObliqueVUps + 
        "','" + busObject.LegClimbs + "','" + 
        busObject.MasonTwists + "')"; 

       if (aConnection.State == ConnectionState.Closed) 
       { 
        aConnection.Open(); 
       } 

       OleDbCommand cmd = aConnection.CreateCommand(); 
       cmd.CommandText = sSQLCommand; 
       // Execute the SQL command 
       cmd.ExecuteNonQuery(); 
       aConnection.Close(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      }     
     }   
    } 
} 
+0

聲音像數據沒有被保存。你確定你打開相同的Access數據庫嗎? – Tim 2012-04-18 00:53:51

+0

@Tim - 是的,我確定。我將數據庫保存在解決方案的調試文件中,並且從那裏開放。我目前只建立了一張桌子,所以我知道我打開了正確的桌子。 – 2012-04-18 01:11:36

+0

然後它聽起來像數據沒有被保存。發佈您的保存方法的代碼,我們可能會幫助更多。 – Tim 2012-04-18 01:12:56

回答

0

一切看起來不錯!我會在你的Console.WriteLine(ex.ToString())上設置一個斷點。並看看你是否錯過了一個例外

+0

好吧,我會那樣做的。謝謝。我想這意味着當我從解決方案調試文件夾打開Access數據庫文件時,我應該能夠看到表中的數據。 – 2012-04-18 01:52:08

+1

好吧,如果你得到某種異常你不會看到數據庫中的數據,你需要看看你是否得到一個異常 – 2012-04-18 01:54:00

+0

@ Micah Armantrout - 我明白了。我發了一個在DB表中的字段名稱。感謝您的幫助。我永遠不會知道查看Console.WriteLine(ex.ToString())行來檢查這個問題。 – 2012-04-18 02:06:50