C#将数据存储到MS-Access数据库中

时间:2013-11-21 11:12:35

标签: c# database ms-access

我需要编写C#代码以将数据插入MS-Access数据库。程序添加它,但如果我关闭应用程序,数据库将重置。 数据库位于debug x86文件夹中

这是我添加数据的代码

provider = "Provider=Microsoft.ACE.OLEDB.12.0"; 

applicatiePad = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"));
pad = "Data Source=" + applicatiePad + "/Geluidsfragmentendb.accdb";
connectionString = provider + ";" + pad;

connection = new OleDbConnection(connectionString);

...

public bool VoegToe(int geluidsfragmentnr,
    string Titel,
    string bestandsnaam,
    int min,
    int sec)
{
    string time = min + ":" + sec;

    DateTime tijd = Convert.ToDateTime(time);

    String voegGfToe = "INSERT INTO Geluidsfragment (GeluidsfragmentID, Titel, bestandsnaam, tijdsduur) VALUES (" + geluidsfragmentnr + ",'" + Titel + "'" + ",'"+ bestandsnaam + "','" + tijd + "')";

    OleDbCommand command = new OleDbCommand(voegGfToe, connection);

    try
    {
        connection.Open();
        command.ExecuteNonQuery();
        return true;
    }
    catch ( Exception e)
    {
        MessageBox.Show(e.Message);
        return false;
    }
    finally
    {
        connection.Close();
    }
}

0 个答案:

没有答案