c#无法将数据插入数据库

时间:2023-02-06 15:42:48

I tired to my code, i have no idea whats wrong with my code please help me to fix this This code should insert the data into database, here im using ms access

我厌倦了我的代码,我不知道我的代码有什么问题请帮我修复这个代码应该将数据插入数据库,这里我使用ms访问

this is my file staff.cs

这是我的文件staff.cs

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

namespace TestStaff
{
    public partial class Staff : Form
    {
        private OleDbConnection Kei = new OleDbConnection();
        public Staff()
        {
            Kei.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D://amel ioop//SalonDatabase.accdb;
            Persist Security info=False;";
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }


        private void Staff_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            new StaffClass().fefefe (Name.Text, DateofBirth.Text, Gender.Text, ICNumber.Text, Email.Text, Mobile.Text, Position.Text);
        }
    }
}

this the other file staffclass.cs

这是另一个文件staffclass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Windows.Forms;

namespace TestStaff
{

    class StaffClass
    {
        private OleDbConnection connection;
        private OleDbCommand _command;
        private OleDbDataReader _reader;
        private string _NAME, _DOB, _EM, _IC, _GE, _MO, _PO;


        public OleDbConnection Connection
        {
            get
            {
                return connection;
            }
        }
        public OleDbCommand Command
        {
            get
            {
                return _command;
            }
            set
            {
                _command = value;
            }
        }
        public OleDbDataReader Reader
        {
            get
            {
                return _reader;
            }
            set
            {
                _reader = value;
            }
        }
        public string NAME
        {
            get
            {
                return _NAME;
            }
            set
            {
                _NAME = value;
            }
        }
        public string IC
        {
            get
            {
                return _IC;
            }
            set
            {
                _IC = value;
            }
        }
        public string MO
        {
            get
            {
                return _MO;
            }
            set
            {
                _MO = value;
            }
        }
        public string GE
        {
            get
            {
                return _GE;
            }
            set
            {
                _GE = value;
            }
        }

        public string EM
        {
            get
            {
                return _EM;
            }
            set
            {
                _EM = value;
            }
        }

        public string PO
        {
            get
            {
                return _PO;
            }
            set
            {
                _PO = value;
            }
        }

        public string DOB
        {
            get
            {
                return _DOB;
            }
            set
            {
                _DOB = value;
            }
        }


         public StaffClass()
        {
            connection = new OleDbConnection();
            connection.ConnectionString = connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪D://amel ioop//SalonDatabase.accdb;
            Persist Security Info=False;";
            connection.Open();
        }

         public bool fefefe(string _NAME, string _DOB, string _IC, string _MO, string _GE, string _EM, string _PO)
         {
             try
             {
                 OleDbCommand command = new OleDbCommand();
                 command.Connection = connection;
                 command.CommandText = "insert into Staff (Name, DateofBirth, ICNumber, MobileNumber, Gender, EmailAddress, Position) values (@_NAME, @_DOB, @_IC, @_MO, @_GE, @EM, @_PO)";
                 command.Parameters.AddWithValue("@_NAME", NAME);
                 command.Parameters.AddWithValue("@_DOB", DOB);
                 command.Parameters.AddWithValue("@_IC", IC);
                 command.Parameters.AddWithValue("@_MO", MO);
                 command.Parameters.AddWithValue("@_GE", GE);
                 command.Parameters.AddWithValue("@_EM", EM);
                 command.Parameters.AddWithValue("@_PO", PO);
                 command.ExecuteNonQuery();
                 MessageBox.Show("Data Saved");
                 connection.Close();
             }
             catch (Exception ex)
             {
                 MessageBox.Show("Error" + ex);
             }
             return true;
         }



    }
}

1 个解决方案

#1


First thing I notice is that you are not assigning your property values. You are passing in _NAME to fefefefe but I don't see where you are assigning the Property NAME to the value _NAME.

我注意到的第一件事是你没有分配你的财产价值。您正在将_NAME传递给fefefefe,但我看不到您将属性名称分配给值_NAME的位置。

A quick fix would be to use the parameters of the function instead of the property values. (ex)

快速修复将使用函数的参数而不是属性值。 (EX)

command.Parameters.AddWithValue("@_NAME", _NAME);
command.Parameters.AddWithValue("@_DOB", _DOB);
command.Parameters.AddWithValue("@_IC", _IC);
command.Parameters.AddWithValue("@_MO", _MO);
command.Parameters.AddWithValue("@_GE", _GE);
command.Parameters.AddWithValue("@_EM", _EM);
command.Parameters.AddWithValue("@_PO", _PO);

Also, are you getting any type of exceptions?

此外,你有任何类型的例外吗?

#1


First thing I notice is that you are not assigning your property values. You are passing in _NAME to fefefefe but I don't see where you are assigning the Property NAME to the value _NAME.

我注意到的第一件事是你没有分配你的财产价值。您正在将_NAME传递给fefefefe,但我看不到您将属性名称分配给值_NAME的位置。

A quick fix would be to use the parameters of the function instead of the property values. (ex)

快速修复将使用函数的参数而不是属性值。 (EX)

command.Parameters.AddWithValue("@_NAME", _NAME);
command.Parameters.AddWithValue("@_DOB", _DOB);
command.Parameters.AddWithValue("@_IC", _IC);
command.Parameters.AddWithValue("@_MO", _MO);
command.Parameters.AddWithValue("@_GE", _GE);
command.Parameters.AddWithValue("@_EM", _EM);
command.Parameters.AddWithValue("@_PO", _PO);

Also, are you getting any type of exceptions?

此外,你有任何类型的例外吗?