数据库查询执行

时间:2017-01-02 08:27:34

标签: c# database sqlite

我在C#中有一个应用程序,数据库是sqlite(.db3)。我有2个功能。

String sConnectionString = "Data Source=mydb.db3;Version=3;Password=123456;";
private void longitudinalProfileToolStripMenuItem_Click(object sender, EventArgs e)
{
    saveProject(iProjectID);
    computeArea(iProjectID);
}

public int saveProject(iProjectID)
{
    SQLiteCommand sqlite_cmd;
    SQLiteCommand sqlite_cmd_PostWork;
    SQLiteCommand sqlite_cmd_Design;
    sqlite_conn = new SQLiteConnection(sConnectionString);
    sqlite_cmd.CommandText = "Delete from prework where pid = " + iProjectID + ";";
    sqlite_cmd.ExecuteNonQuery();
    sqlite_cmd.CommandText = "Insert into prework (pid, chainage, date) values (" + iProjectID + "," + fChainagePostwork + ",'" + dateTimePicker1.Value.Date.ToString("yyyy-MM-dd") + "');";
    sqlite_cmd.ExecuteNonQuery();
    sqlite_conn.Close();
return 1;
}

public int computeArea(iProjectID)
{
    SQLiteCommand sqlite_cmd;
    SQLiteCommand sqlite_cmd_PostWork;
    SQLiteCommand sqlite_cmd_Design;
    sqlite_conn = new SQLiteConnection(sConnectionString);
    sqlite_cmd.CommandText = "Delete from postwork where pid = " + iProjectID + ";";
    sqlite_cmd.ExecuteNonQuery();
    sqlite_cmd.CommandText = "Insert into postwork (pid, chainage, date) values (" + iProjectID + "," + fChainagePostwork + ",'" + dateTimePicker1.Value.Date.ToString("yyyy-MM-dd") + "');";
    sqlite_cmd.ExecuteNonQuery();
    sqlite_conn.Close();
return 1;
}

saveProject()函数正常工作。 但是在computeArea()函数中,查询不会显示任何错误,但数据库/表格不会更新/插入/删除。

如果我第二次调用longitudinalProfileToolStripMenuItem_Click(对象发送者,EventArgs e),那么它可以正常工作。

但在之前的时间它运作正常。 有什么问题?

1 个答案:

答案 0 :(得分:-2)

您可以像这样使用'使用块',

String sConnectionString = "Data Source=mydb.db3;Version=3;Password=123456;";
private void longitudinalProfileToolStripMenuItem_Click(object sender, EventArgs e)
{
    saveProject(iProjectID);
    computeArea(iProjectID);
}

saveProject(iProjectID)
{
    SQLiteCommand sqlite_cmd;
    SQLiteCommand sqlite_cmd_PostWork;
    SQLiteCommand sqlite_cmd_Design;
    using (sqlite_conn = new SQLiteConnection(sConnectionString))
    {
        sqlite_conn.Open();
        sqlite_cmd.CommandText = "Delete from prework where pid = " + iProjectID + ";";
        sqlite_cmd.ExecuteNonQuery();
        sqlite_cmd.CommandText = "Insert into prework (pid, chainage, date) values (" + iProjectID + "," + fChainagePostwork + ",'" + dateTimePicker1.Value.Date.ToString("yyyy-MM-dd") + "');";
        sqlite_cmd.ExecuteNonQuery();
    }
}

computeArea(iProjectID)
{
    SQLiteCommand sqlite_cmd;
    SQLiteCommand sqlite_cmd_PostWork;
    SQLiteCommand sqlite_cmd_Design;

    using (sqlite_conn = new SQLiteConnection(sConnectionString))
    {
        sqlite_conn.Open();
        sqlite_cmd.CommandText = "Delete from postwork where pid = " + iProjectID + ";";
        sqlite_cmd.ExecuteNonQuery();
        sqlite_cmd.CommandText = "Insert into postwork (pid, chainage, date) values (" + iProjectID + "," + fChainagePostwork + ",'" + dateTimePicker1.Value.Date.ToString("yyyy-MM-dd") + "');";
        sqlite_cmd.ExecuteNonQuery();
    }
}