简体   繁体   English

安装后,带有 Access 数据库 (accdb) 的 c# windows 应用程序在其他计算机上不起作用

[英]The c# windows app with Access database (accdb) doesn't work on other computers after installation

I want to insert a text in my Access database (accdb) in C#-windows application project.我想在 C#-windows 应用程序项目中的 Access 数据库 (accdb) 中插入文本。 The "accdb" . “accdb”。 It works correctly on my computer ONLY if i run the exe file which situated in my project source folder but the problem is that when i build a setup file and installed it and run the software, it opens but when i click on the insert button, it couldn't work.只有当我运行位于我的项目源文件夹中的 exe 文件时,它才能在我的计算机上正常工作,但问题是当我构建一个安装文件并安装它并运行软件时,它会打开,但是当我单击插入按钮时,它无法工作。 The problem is with the database (location/access) but i dont't know how can i solve it.问题出在数据库(位置/访问)上,但我不知道如何解决。 Does anyone know how to solve this problem?有谁知道如何解决这个问题?

The error is: Unhandled exception has occurred in your application.错误是:您的应用程序中发生了未处理的异常。 ... "Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately. See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box. ...“您的应用程序中发生了未处理的异常。如果您单击继续,应用程序将忽略此错误并尝试继续。如果您单击退出,应用程序将立即关闭。有关调用 just 的详细信息,请参阅此消息的末尾-in-time (JIT) 调试而不是此对话框。

************** Exception Text ************** System.Data.OleDb.OleDbException (0x80004005): Operation muss eine aktualisierbare Abfrage verwenden. ************** 异常文本 ************** System.Data.OleDb.OleDbException (0x80004005):操作 muss eine aktualisierbare Abfrage verwenden。 at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(SystemObject.&. OleDb.OleDbCommand.ExecuteCommand(CommandBehavior行为,Object&executeResult)

在此处输入图片说明 在此处输入图片说明

Here is my code:这是我的代码:

 public Form1()
    {
        InitializeComponent();
    }

    public static string GetDBConnection()
    {
        try
        {
            string dbExecPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Test11.accdb");
            return $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={ dbExecPath }";
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }

   OleDbConnection con = new OleDbConnection(GetDBConnection());
   OleDbCommand cmd;

    private void button1_Click(object sender, EventArgs e)
    {

        OleDbCommand cmd = new OleDbCommand("insert into[Table](name, code) VALUES(@name, @code)", con);
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@name", textBox1.Text);
             cmd.Parameters.AddWithValue("@code", textBox2.Text);

            con.Open();

            System.Windows.Forms.MessageBox.Show("An Item has been successfully added", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

        int i = 0;
        i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            MessageBox.Show("Inserted");
            DisplayData();
        }

        else
        {
            MessageBox.Show("Not Inserted");
        }
        con.Close();
        ///////
    }

In your code you use Assembly.getExecutingAssembly().Location to specify the location of you .accdb-file.在您的代码中,您使用Assembly.getExecutingAssembly().Location来指定 .accdb 文件的位置。 As soon as you change the location of your executable (.exe) the value that Assembly.getExecutingAssembly().Location returns changes ( See MS docs ).一旦您更改了可执行文件 (.exe) 的Assembly.getExecutingAssembly().LocationAssembly.getExecutingAssembly().Location返回的值就会发生变化( 请参阅 MS 文档)。 So you need to make sure your executable and your .accdb-file are always in the same folder.所以你需要确保你的可执行文件和你的 .accdb 文件总是在同一个文件夹中。 Otherwise a connection to your Access database cannot be established.否则无法建立到 Access 数据库的连接。

You could add a message box that shows the path your program expects the database to be located.您可以添加一个消息框,显示您的程序希望找到数据库的路径。 Afterwards you can check whether your database really is located in the right directory.之后您可以检查您的数据库是否确实位于正确的目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM