简体   繁体   English

C#“插入到” MS Access数据库未插入

[英]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. 因此,我正在处理数据库程序,它需要使用INSERT INTO命令将日志插入数据库中,但是执行查询后该表仍然为空。 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>,...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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