简体   繁体   中英

Microsoft Access Database, Insert, Update and Delete in Visual Studio with C#

I'm trying to insert a row to a table in Microsoft Access Database via Visual Studio 2015 an c#.

the Scenario is: a class called: QuestionData and a class Question . A table exams , and table questions .

my code is:

public static bool addQuestion(QuestionData quesData)
    {
        if (quesData == null || quesData.question == null) { return false; }
        List<QuestionData> questionsData = new List<QuestionData>();

        string connetionString = null;
        OleDbConnection cnn;
        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=database.mdb;";
        cnn = new OleDbConnection(connetionString);
        try
        {
            string insertCmd = "INSERT INTO questions (exam_id, [name], question_text, withImage, question_correctAnswer, answer_1, answer_2, answer_3, answer_4) VALUES (@exam_id, @name, @question_text, @withImage, @question_correctAnswer, @answer_1, @answer_2, @answer_3, @answer_4)";
            OleDbCommand cmd = new OleDbCommand(insertCmd);

            cmd.Parameters.Add("@exam_id", OleDbType.Numeric).Value = quesData.examId;
            cmd.Parameters.Add("@name", OleDbType.LongVarChar).Value = quesData.question.getName();
            cmd.Parameters.Add("@questionText", OleDbType.LongVarChar).Value = quesData.question.getQuestionText();
            cmd.Parameters.Add("@withImage", OleDbType.Boolean).Value = quesData.question.isImageEnabled();
            cmd.Parameters.Add("@question_CorrectAnswer", OleDbType.Numeric).Value = quesData.question.getCorrectAnswer();
            cmd.Parameters.Add("@answer_1", OleDbType.LongVarChar).Value = quesData.question.getAnswer(0);
            cmd.Parameters.Add("@answer_2", OleDbType.LongVarChar).Value = quesData.question.getAnswer(1);
            cmd.Parameters.Add("@answer_3", OleDbType.LongVarChar).Value = quesData.question.getAnswer(2);
            cmd.Parameters.Add("@answer_4", OleDbType.LongVarChar).Value = quesData.question.getAnswer(3);

            cmd.Connection = cnn;
            cnn.Open();
            int result = cmd.ExecuteNonQuery();
            cnn.Close();

            return true;
        }

        catch (Exception ex)
        {
            MessageBox.Show("Exception: \n Source: " + ex.Source + "\n Message: " + ex.Message);
            cnn.Close();
            return false;
        }
    }

Everything works fine, with no compiling or run-time errors, and cmd.ExcuteNonQuery(); line returns positive value (1) , but nothing happens to database file. but Read Query works fine and does its job.

I searched google, and tried every single solution on the internet, but no thing changed the result.

I appreciate any help, and any guide to solve the problem.

With Access the paramaters should be marked as "?". They don't use a name but the order they are added to the Parameters Collection (a fake name is still needed :-))

Try to replace

string insertCmd = "INSERT INTO questions (exam_id, [name], question_text, withImage, question_correctAnswer, answer_1, answer_2, answer_3, answer_4) VALUES (@exam_id, @name, @question_text, @withImage, @question_correctAnswer, @answer_1, @answer_2, @answer_3, @answer_4)";

with

string insertCmd = "INSERT INTO questions (exam_id, [name], question_text, withImage, question_correctAnswer, answer_1, answer_2, answer_3, answer_4) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

Check that's you are connecting to the right files by using a fixed path. It could be confusing.

By the way you better have to use the using keyword for the Connection object, you will be sure that it will be properly closed when you go out of the using statement (normal way and exception too). This point is only a good practice, not a source of error in your case.

using cnn = new OleDbConnection(connetionString)
{
  cnn.open
  //Query the DB
}

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