建立项目成功与Access数据库,可以搜索数据,但不能删除记录 [英] Building project succeed with Access database, can search the data but cannot delete the record

查看:167
本文介绍了建立项目成功与Access数据库,可以搜索数据,但不能删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是学习Visual Studio C#2个月。最近,我编写的代码可以连接到Microsoft Access 2007,它运行顺利(它可以显示数据,并可以删除记录,我预期)然而,当我建立项目,它仍然显示数据,但不能删除记录(它显示错误消息框未处理的异常已发生在您的应用程序中。如果您单击继续,应用程序将忽略此错误,并尝试继续,如果您单击退出,应用程序将立即关闭)
我不知道什么错了。我尝试了很多方法的解决方案,但还没有成功。
我将项目上传到4shared.com,所以请通过此链接下载 - >
http://www.4shared.com/zip/bxhZC3Wp/WindowsFormsApplication32.html

I just learn Visual Studio C# for 2 months. Recently, I write the code that can connect with Microsoft Access 2007 and it run smoothly (it can show data and can delete record as I expect) However, when I build the project, it still show data but cannot delete record.(it show error message box "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") I don't know what's wrong with it. I try many ways for the solution but not yet succeed. I upload the project to 4shared.com so please download by this link --> http://www.4shared.com/zip/bxhZC3Wp/WindowsFormsApplication32.html?

下载后,请打开并尝试运行代码(文件名WindowsFormsApplication32)
你会看到3个文本框,类型1并点击button1,它将在textbox2中显示名称,在textbox3中显示城市。现在点击button2删除记录,可以看到它将成功删除记录。然后,打开文件夹iii>调试,安装程序(文件名iii),并打开程序(可能位于C:\Program Files(x86)\默认公司名称\iii)。你可以尝试与运行代码时相同的方式,但单击button2后删除记录,它将显示messagebox错误(请通过此链接下载图片的错误消息 - > http://www.4shared.com/photo/FgUODfoW/error_messagebox.html ?)
任何人请帮助

After download, please open and try running the code (file name WindowsFormsApplication32) you will see 3 textboxes, type 1 and click button1, it will show name in textbox2 and city in textbox3. Now click button2 to delete the record, you can see that it will delete the record successfully. Then, open the folder iii>Debug, install the program (file name iii),and open the program (may located at C:\Program Files (x86)\Default Company Name\iii). You can try the same way as when you run the code but after click button2 to delete the record, it will show messagebox error (please download the picture of the message error by this link --> http://www.4shared.com/photo/FgUODfoW/error_messagebox.html? ) Anyone please help me.

PS。这是我如何构建项目。

PS. This is how I Build the project.


  1. 右键单击打开Visual Studio2010,选择以
    管理员身份运行。

  2. 打开我的项目

  3. '文件'>'添加'>'新建项目'

  4. 在添加新项目窗口中,

  5. 在文件系统选项卡中,右键单击应用程序文件夹>添加>项目输出...'

  6. 在文件系统选项卡中,右键单击应用程序文件夹>添加>文件...,然后选择我的Microsoft Access 2007文件。 li>
  7. 在解决方案资源管理器窗口中,右键单击我的项目并选择'Build'
    这是否可以正确导入数据库到我的项目?

  1. Open Visual Studio2010 by Right Click and select 'Run as administrator'.
  2. Open my project
  3. 'File' > 'Add' > 'New Project'
  4. In 'Add New Project' Window, select 'Other Project Types'>'Setup and Deployment'> 'Visual Studio Installer' select 'Setup Project'
  5. In 'File System' tab, right click 'Application Folder'> 'Add' > 'Project Output...'
  6. In 'File System' tab, right click 'Application Folder'> 'Add' > 'File...' and select my Microsoft Access 2007's file.
  7. in 'Solution Explorer' window, right click my project and select 'Build' Is this way can import the database to my project correctly?

下面是代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication32
{
public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }
    private OleDbConnection connection;
    private OleDbDataAdapter adapter;

    private OleDbCommand command;
    private string sql;

    private void Form1_Load(object sender, EventArgs e)
    {
        string con = @"Provider=Microsoft.ACE.OLEDB.12.0;
                        Data Source=|DataDirectory|\bbb.accdb;
                        Persist Security Info=False;";
        connection = new OleDbConnection(con);
        if (connection.State == ConnectionState.Closed)
            connection.Open();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        sql = "SELECT * FROM test WHERE id=" + textBox1.Text;

        command = new OleDbCommand(sql, connection);

        adapter = new OleDbDataAdapter(command);
        DataSet data = new DataSet();
        adapter.Fill(data, "abc");
        if (data.Tables["abc"].Rows.Count == 0) return;

        else
        {

            textBox2.Text = Convert.ToString(data.Tables["abc"].Rows[0]["Name"]);
            textBox3.Text = Convert.ToString(data.Tables["abc"].Rows[0]["City"]);
        }


    }

    private void button2_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show("are you sure to delete", "delete", MessageBoxButtons.OKCancel);
        if (result == DialogResult.Cancel) return;
        sql = "DELETE FROM test WHERE ID=" + textBox1.Text;
        command = new OleDbCommand(sql, connection);
        int r = (int)command.ExecuteNonQuery();
        if (r > 0)
        {
            MessageBox.Show("already delete");
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }
        else MessageBox.Show("error to delete");


        }
    }
}


推荐答案

错误是因为文件夹权限。如果您使用的是Window 7,安装应用程序后,转到已安装的目录并右键单击可执行文件。单击以管理员身份运行,现在尝试删除记录。

The error is because of the folder permissions. If you are using Window 7, after you installed the application, go to the installed directory and right click on the executable. Click on "Run as administrator" and now try deleting the record. This time it will be successful.

在任何操作系统中使用的备用方法
向本地系统用户(MYPC \Users)提供修改权限,然后重试。它会工作。

Alternate method to work in any OS Provide modify permission to local system Users (MYPC\Users) and try again. It will work.

这篇关于建立项目成功与Access数据库,可以搜索数据,但不能删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆