尝试使用Windows窗体更新Access DB中的字段时,获取OleDbException是未解决的错误

时间:2018-01-04 08:31:45

标签: c# asp.net .net ms-access

我已经编写了一个代码来插入,更新,删除,查看和搜索访问数据库中的字段,使用下面的窗体表格是我的代码

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\saurabh.ad.sharma\Documents\Visual Studio 2012\Projects\APPLICATION\MyDb.accdb");
        int count = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into table1 (Eid,Name,City)values('" + textBox3.Text + "','" + textBox1.Text + "','"+textBox2.Text+"')";
            cmd.ExecuteNonQuery();
            con.Close();
            textBox1.Text = "";
            textBox2.Text = "";
            MessageBox.Show("record inserted successfully");

        }

        private void button4_Click(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from table1";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "delete from table1 where name='"+textBox1.Text+"'";
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("record deleted successfully");
        }

        private void button3_Click(object sender, EventArgs e)
        {

                con.Open();
                OleDbCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "update table1 set Name='" + textBox1.Text + "' City='" + textBox2.Text + "' where Eid='" + textBox3.Text + "'";
                cmd.ExecuteNonQuery();

                MessageBox.Show("record updates successfully");
                con.Close();


        }


        private void button5_Click(object sender, EventArgs e)
        {
            count = 0;
            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from table1 where name='"+textBox1.Text+"' ";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(dt);
            count = Convert.ToInt32(dt.Rows.Count.ToString());
            dataGridView1.DataSource = dt;
            con.Close();


            if (count == 0)
            {
                MessageBox.Show("reocrd not found");
            }




        }
    }
}

我在尝试在上面的代码中更新Access DB中的字段时得到OleDbException未处理 -

  private void button3_Click(object sender, EventArgs e)
        {

                con.Open();
                OleDbCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "update table1 set Name='" + textBox1.Text + "' City='" + textBox2.Text + "' where Eid='" + textBox3.Text + "'";
                cmd.ExecuteNonQuery();

                MessageBox.Show("record updates successfully");
                con.Close();


        }

以下是我的窗体图像

enter image description here

我无法找出错误的帮助。

0 个答案:

没有答案