C# auf Accessdatei (accdb) zugreiffen

  • C#

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von outlop.

    C# auf Accessdatei (accdb) zugreiffen

    Hallo Leute,

    Ich habe mir ein Beispielprogramm heruntergeladen wo mit C# auf eine Access-Datenbank zugegriffen wird.
    Im Beispiel wird eine .mdb Accessdatei verwendet, jedoch möchte ich eine .accdb verwenden.

    Nachdem ich die mdb in accdb abgespeichert habe und den Pfad im Programm angepasst habe erschien folgende Meldung:

    OleDbException was unhandled. (mit Verweis auf Zeile bookConn.Open() )

    Nicht erkennbares Datenbankformat. C:\Test\book.accdb

    Wie kann ich eine .accdb Datei verwenden ?

    Danke für eure Hilfe

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using System.Data.OleDb;
    10. namespace TutorialConnectToAccessDB
    11. {
    12. public partial class FormMain : Form
    13. {
    14. private OleDbConnection bookConn;
    15. private OleDbCommand oleDbCmd = new OleDbCommand();
    16. //parameter from mdsaputra.udl
    17. private String connParam = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=book.accdb;Persist Security Info=False";
    18. public FormMain()
    19. {
    20. //create connection using parameter from mdsaputra.udl
    21. bookConn = new OleDbConnection(connParam);
    22. InitializeComponent();
    23. }
    24. private void buttonInsert_Click(object sender, EventArgs e)
    25. {
    26. bookConn.Open();
    27. oleDbCmd.Connection = bookConn;
    28. oleDbCmd.CommandText = "insert into book (bookName, description) values ('" + this.textBoxBookName.Text + "','" + this.textBoxDescription.Text + "');";
    29. int temp = oleDbCmd.ExecuteNonQuery();
    30. if (temp > 0)
    31. {
    32. textBoxBookName.Text = null;
    33. textBoxDescription.Text = null;
    34. MessageBox.Show("Record Successfuly Added");
    35. }
    36. else
    37. {
    38. MessageBox.Show("Record Fail to Added");
    39. }
    40. bookConn.Close();
    41. }
    42. }
    Provider=Microsoft.Jet.OLEDB.4.0 <-- mdb
    Provider=Microsoft.ACE.OLEDB.12.0 <-- accdb
    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."