简体   繁体   中英

C# delete database ms.access

I have problem delete data microsoft access database with C# I use this method to add data to microsoft access database

public static int autoIncrement(string kalimatSql)
{
    int lastIndex = -1;
    dbCon.Open();
    dbCmd.CommandText = kalimatSql;
    dbCmd.ExecuteNonQuery();
    dbCmd.CommandText = "Select @@Identity";
    lastIndex = Convert.ToInt32(dbCmd.ExecuteScalar());
    dbCon.Close();
    return lastIndex;
}

I call that method with

myQuiz.id = Global.autoIncrement("INSERT INTO Quizzes (Name) VALUES ('" + myQuiz.name + "')");
Global.quizzes.Add(myQuiz);

I wonder how to delete data from database? if I add with that way?
So far I already try this way

public static int deleteData(string kalimatSQL)
{
    int lastIndex = -1;
    dbCon.Open();
    dbCmd.CommandText = kalimatSQL;
    dbCmd.ExecuteNonQuery();
    dbCon.Close();
    return lastIndex;
}

And I call delete method with this way

if ( listBoxQuizzes.SelectedIndex != -1)  {
    Global.deleteData("DELETE FROM Quizzes WHERE name=" +listBoxQuizzes.SelectedItem.ToString()); 
}

But give error result

"C# Syntax error (missing operator) in query expression 'name = quiz002' " can someone help me fix it?

You are missing single quotes around name value ( '' )

It should be like this:

Global.deleteData("DELETE FROM Quizzes WHERE name = '" + listBoxQuizzes.SelectedItem.ToString() + "'");

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