SQLite查询未在C#上执行

时间:2015-02-12 09:40:06

标签: c# sqlite

代码不会产生异常,看起来运行正常。但在运行它并检查表后,我发现没有插入行。我使用navicat进行检查,这不是问题所在。我仔细检查了所有正确的表名和字段名。怎么了?

try
{
    SQLiteConnection conn = new SQLiteConnection("Data Source=path;Version=3;FailIfMissing=True");

    conn.Open();
    String sql = "INSERT INTO sales (cust_id, date, cost, price) VALUES ('0', '" + String.Format("{0:u}", DateTime.Now.Date).Split(' ')[0] + "', '" + cost.ToString() + "', '12')";
    SQLiteCommand command = new SQLiteCommand(sql, conn);

    conn.Close();
}
catch (Exception err)
{
    MessageBox.Show(err.Message.ToString());
}

1 个答案:

答案 0 :(得分:1)

添加:

command.ExecuteNonQuery();

代码:

...
SQLiteCommand command = new SQLiteCommand(sql, conn);
command.ExecuteNonQuery();

此外,最好在查询中使用参数