user1494994
user1494994

Reputation:

C# `INSERT INTO` MS Access db not inserting

so I'm working on a database program and it requires to insert a Log into the database using INSERT INTO command, however the table remains empty after executing the query. Please help! Thanks~ Here are the codes.

    //Predefined connection string
    OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\xxx\Desktop\data.mdb;Jet OLEDB:Database Password=xxx;");




    private void Login_Load(object sender, EventArgs e)
    {
        Log("StartUp", "System", "NULL", "System StartUp");
    }

    public void Log(string Type, string User, string Table_affected, string Remarks)
    {

        string NowDateStamp = string.Format("{0}/{1}/{2}", 
                              DateTime.Now.Day.ToString(), 
                              DateTime.Now.Month.ToString(), 
                              DateTime.Now.Year.ToString());
        string NowTimeStamp = string.Format("{0}:{1}:{2}",
                              DateTime.Now.Hour.ToString(),
                              DateTime.Now.Minute.ToString(),
                              DateTime.Now.Second.ToString());
        string tempSQL = string.Format("INSERT INTO 'Log' VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}')",
                                        NowDateStamp,
                                        NowTimeStamp,
                                        Type,
                                        User,
                                        Table_affected,
                                        Remarks);
        vcon.Open();
        OleDbCommand vcom = new OleDbCommand(tempSQL, vcon);
        vcom.ExecuteNonQuery();
        MessageBox.Show("Done"); // <-- This MessageBox isn't even showing.
        vcon.Close();
    }

So basically the program will start up then log the time, however it seemed done nothing to the database, anyone can help me out? Thanks!

Upvotes: 0

Views: 1648

Answers (3)

Brad
Brad

Reputation: 12245

In addition to what others have said I would guess that you have a syntax error too. You should be specifying into which columns you want your values to be inserted.

INSERT INTO Log (<column1>,<column2>,...)
VALUES (<Value to insert into column1>, <Value to insert into column 2>,...)

Upvotes: 0

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

Replace this:

INSERT INTO 'Log' VALUES 

With this one:

INSERT INTO Log VALUES 

Upvotes: 0

Guffa
Guffa

Reputation: 700162

Executing the query causes an exception, but somewhere that exception is caught and ignored.

You are using a string literal where the table name should be. It should either be just the identifier:

INSERT INTO Log VALUES ...

or the identifier inside square brackets:

INSERT INTO [Log] VALUES ...

Upvotes: 6

Related Questions