【问题标题】:Accessing saved data from database program从数据库程序访问保存的数据
【发布时间】:2012-04-18 09:47:41
【问题描述】:

这可能是一个愚蠢的问题,如果是,那么我道歉。我有一个程序,其中用户将数据输入到 Windows 窗体并单击一个按钮。该按钮将输入的数据保存到 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());
            }                
        }        
    }
}

【问题讨论】:

  • 听起来数据没有被保存。您确定打开的是同一个 Access 数据库吗?
  • @Tim - 是的,我确定。我将数据库保存在解决方案的调试文件中,我正在从那里打开。到目前为止我只建了一张桌子,所以我知道我打开了正确的桌子。
  • 然后听起来数据没有被保存。发布您的保存方法的代码,我们可能会提供更多帮助。
  • 我会发布一些代码,让我们看看你在做什么 :)
  • 修改debug文件夹下的Access文件名,然后测试看看有没有报错。

标签: c# ms-access ado.net


【解决方案1】:

一切看起来都不错!我会在你的 Console.WriteLine(ex.ToString()); 上设置一个断点看看你是否遗漏了异常

【讨论】:

  • 好的,我会这样做的。谢谢你。我想这意味着当我从解决方案调试文件夹中打开 Access 数据库文件时,我应该能够看到表中的数据。
  • 好吧,如果您遇到某种异常,您将看不到数据库中的数据,您需要查看您是否遇到异常
  • @Micah Armantrout - 我明白了。我发现了 DB 表中的一个字段名称。谢谢您的帮助。我永远不会知道查看 Console.WriteLine(ex.ToString()) 行来检查这个问题。
猜你喜欢
  • 2013-10-15
  • 2011-05-09
  • 2016-06-28
  • 2019-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
相关资源
最近更新 更多