Adding data in the database using microsoft access and OleDb in C#

18,348

Solution 1

Solved

public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Project\Learning\Visual C#\Form\WindowsFormsApplication2\WindowsFormsApplication2\Test.mdb";
        conn.Open();
        string Name = textBox1.Text;
        OleDbCommand cmmd = new OleDbCommand("INSERT INTO table1 (student) Values(@Name)", conn);
        if (conn.State == ConnectionState.Open)
        {
            cmmd.Parameters.Add("@Name", OleDbType.VarWChar, 20).Value = Name;
            try
            {
                cmmd.ExecuteNonQuery();
                MessageBox.Show("DATA ADDED");
                conn.Close();
            }
            catch (OleDbException expe)
            {
                MessageBox.Show(expe.Message);
                conn.Close();
            }
        }
        else
        {
            MessageBox.Show("CON FAILED");
        }
    }

Solution 2

May be this code can help you

      OleDbConnection dbConnection = new OleDbConnection(CONNECTION_STRING);

        string commandString = 
        "INSERT INTO MeetingEntries (Subject, Location, Start Date, End Date, Enable   Alarm, Repeat Alarm, Reminder, Repetition Type)" + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        OleDbCommand commandStatement = new OleDbCommand(commandString, dbConnection);

        commandStatement.Parameters.Add("@Subject", OleDbType.VarWChar, 30).Value = currentEntry.Subject;
        commandStatement.Parameters.Add("@Location", OleDbType.VarWChar, 50).Value = currentEntry.Location;
        commandStatement.Parameters.Add("@Start Date", OleDbType.Date, 40).Value = currentEntry.StartDateTime.Date;
        commandStatement.Parameters.Add("@End Date", OleDbType.Date, 40).Value = currentEntry.EndDateTime.Date;
        commandStatement.Parameters.Add("@Enable Alarm", OleDbType.Boolean, 1).Value = currentEntry.IsAlarmEnabled;
        commandStatement.Parameters.Add("@Repeat Alarm", OleDbType.Boolean, 1).Value = currentEntry.IsAlarmRepeated;
        commandStatement.Parameters.Add("@Reminder", OleDbType.Integer, 2).Value = currentEntry.Reminder;
        commandStatement.Parameters.Add("@Repetition Type", OleDbType.VarWChar, 10).Value = currentEntry.Repetition;

        dbConnection.Open();
        commandStatement.ExecuteNonQuery();
Share:
18,348
Erfan Jazeb Nikoo
Author by

Erfan Jazeb Nikoo

I am a Robotics researcher, programmer, and an Electronics Engineer who likes embedded system designing and programming for high tech devices. I have conducted research on embedded systems and Artificial Intelligence in several research activities at Mechatronics Research Laboratory as a Senior Researcher for seven years. I focused on machine learning, Artificial Intelligence, Multi-Agent systems and autonomous agent using Java and C/C++ languages on a different branch of Robotics fields. Also, I have experience in mobile applications development with Android and Flutter. My scientific contribution led to achieving top ranks in RoboCup competitions.

Updated on June 17, 2022

Comments

  • Erfan Jazeb Nikoo
    Erfan Jazeb Nikoo almost 2 years

    I am new to OleDb library and I want to add a text form text box into a database with this library. My code:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private OleDbConnection conn = new OleDbConnection();
        private void button1_Click(object sender, EventArgs e)
        {
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Project\Learning\Visual C#\Form\WindowsFormsApplication1\WindowsFormsApplication1\Test.mdb";
            string NAME = textBox1.Text;
            conn.Open();
            OleDbCommand cmmd = new OleDbCommand("INSERT into student(NAME)" + "VALUES(@NAME)", conn);  
            if (conn.State == ConnectionState.Open)
            {
                cmmd.Parameters.Add("@NAME", OleDbType.Char, 20);
                cmmd.Parameters["@NAME"].Value = NAME;
                try
                {
                    cmmd.ExecuteNonQuery();
                    MessageBox.Show("DATA ADDED");
                    conn.Close();
                }
                catch (OleDbException expe)
                {
                    MessageBox.Show(expe.Source);
                }
            }
            else
            {
                MessageBox.Show("CON FAILED");
            }
        }
    }
    

    But it doesn't work. I cannot find a good reference for OleDbCommand in C#.
    How can I use OleDbCommand in my code?

  • JMK
    JMK about 11 years
    Good to see you solved this, if you wait a little while you can mark this as the answer.Happy coding :)