使用删除按钮从.mdb文件中删除 [英] Deleting from a .mdb file with a delete button

查看:108
本文介绍了使用删除按钮从.mdb文件中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家,我正在试图找出为什么我的代码不允许我从.mdb文件中删除一行。它继续说

Hey everyone, i'm trying to figure out why my code isn't allowing for me to delete a line from a .mdb file. it's continuing to say

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Data type mismatch in criteria expression.

。这与我搜索项目时遇到的错误相同。删除该问题的代码是:

. This is the same error I get when I search for an item. The code for deleting that's giving me problems is:

public void Delete()
        {
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Work Projects\\SurveyApp\\employees.mdb");
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("DELETE *FROM employee Where EmpID='" + EmpID + "'", conn);
            cmd.ExecuteNonQuery();

,搜索代码为:

private void searchb_Click(object sender, EventArgs e)
      {
          OleDbConnection con;
          OleDbDataAdapter ad;
          //text test
          if (searchtext.Text == "")
          {
              MessageBox.Show("Plese Enter the ID you are searching for");
          }
          else
          {

              con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Work Projects\\SurveyApp\\employees.mdb");
              ad = new OleDbDataAdapter("SELECT * FROM employee WHERE EmpID='searchtext.text'", con);
              ad.SelectCommand.Parameters.Add("@EmpID", OleDbType.Integer);
              ad.SelectCommand.Parameters["@EmpID"].Value = int.Parse(searchtext.Text);


              DataSet ds = new DataSet();
              ad.Fill(ds, "DGV1");
              DGV1.DataSource = ds.Tables["DGV1"];
              DGV1.Refresh();
              DGV1.Update();

          }
      }





另外我不确定更新的原因我没有工作,我在更新语句中得到语法错误



Also I'm not sure why the update isn't working either I get a syntax error in update statement

public void Update()
        {
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Work Projects\\SurveyApp\\employees.mdb");
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("update employee([EmpID],[Name],[Jobtitle],[Company])Values ('" + EmpID + "','" + Name + "','" + Jobtitle + "','" + Company + "')", conn);
            cmd.ExecuteNonQuery();



非常感谢你能为家伙提供帮助!



我尝试了什么:



我不确定修改时需要更改什么。


Thanks so much for whatever you can help with guys!

What I have tried:

I am not sure what I need to change for the fix.

推荐答案

如果EmpID columnn是一个数字列,并且您传入一个字符串。查询字符串中的'characters \ n'表示SQL中的字符串,而不是数字值。

If EmpID columnn is a numeric column and you're passing in a string. The ' characters \in the query string denote a string in SQL, not a numeric value.
SELECT * FROM employee WHERE EmpID='searchtext.text'





您的查询字符串实际上应该是:



Your query string should actually be:

SELECT * FROM employee WHERE EmpID=@EmpID



基于您传入的参数对象。



您的更新查询也容易受到SQL注入攻击和可能会破坏您的数据库。改为使用参数化查询,就像您尝试为SELECT做的那样。


based on the parameter object you passed in.

Your update query is also prone to SQL Injection Attacks and the possible destruction of your database. Use a parameterized query instead, like you attempted to do for the SELECT.


这篇关于使用删除按钮从.mdb文件中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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