简体   繁体   中英

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!

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 ...

Replace this:

INSERT INTO 'Log' VALUES 

With this one:

INSERT INTO Log VALUES 

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>,...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM