2012-04-18 163 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