带有 Access 数据库的 C# CRUD:无法执行交互不是有效的文件名

问题描述

我对 c# 比较陌生,我在我的大学从事一项工作,其中包括验证交换机,我现在在 CRUD 中工作以编辑、创建和删除数据网格视图中一列的项目,所以这是我的问题。

我使用 try 和 catch 将我的项目连接到数据库,但似乎我在连接中遇到了问题,每次我尝试编辑、删除或创建时,我都会收到一条错误消息,说“不是有效文件名称”。

    private void button5_Click(object sender,EventArgs e)
    {
        string strcon = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + Application.StartupPath + @"C:\\Verificação de Quadros Elétricos\\Verificação de Quadros Elétricos\\Databaseteste.accdb";
        string comando = "INSERT INTO Tabela normas (Drills(according tto the norm EN 61439)values(@Drills(according tto the norm EN 61439))";

        OleDbConnection con = new OleDbConnection(strcon);
        OleDbCommand com = new OleDbCommand(comando,con);

        com.Parameters.Add("@Drills(according tto the norm EN 61439)",OleDbType.VarChar).Value = textBox5.Text;

        try
        {
            con.open();
            com.ExecuteNonQuery();
            MessageBox.Show("Save Well Succeded !");

        }
        catch (Exception E)
        {
            MessageBox.Show(E.Message);
        }
        finally
        {
            con.Close();
        }



    }

这是在列中创建新项目的代码,感谢您的时间。

代码中的一件事引起了我的注意,可能意味着数据库有问题。

Error

数据库上的这个 x 是否意味着它没有连接?

解决方法

(抱歉,好像没有被理解为评论)

private void button5_Click(object sender,EventArgs e)
    {
        string strcon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + 
               @"C:\Data\Databaseteste.accdb";
        string comando = @"INSERT INTO [Tabela Normas] 
           ([Drills(according to the norm EN 61439)])
           values
           (@Drills)";

        using (OleDbConnection con = new OleDbConnection(strcon))
        using (OleDbCommand com = new OleDbCommand(comando,con))
        {

        com.Parameters.Add("@Drills",OleDbType.VarChar).Value = textBox5.Text;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            MessageBox.Show("Save Well Succeded !");

        }
        catch (Exception E)
        {
            MessageBox.Show(E.Message);
        }
    }
}

PS:如果 Application.StartupPath 在 Program Files 下,则该位置是只读的。记住这一点。