0

これはばかげた質問かもしれません。もしそうなら申し訳ありません。ユーザーがデータを 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());
            }                
        }        
    }
}
4

1 に答える 1

0

すべてがよさそうだ!Console.WriteLine(ex.ToString());にブレークポイントを設定します。例外がないかどうかを確認します

于 2012-04-18T01:47:41.313 に答える