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
this is my file 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
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;
}
}
}
Aucun commentaire:
Enregistrer un commentaire