qq_37145772 2021-12-12 01:55 采纳率: 100%
浏览 52
已结题

语句问题,找大大捉虫

img


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

namespace EEE3111_EA_210078707
{
    public partial class Form2 : Form
    {
        private OleDbConnection connection = new OleDbConnection();
        public Form2()
        {
            InitializeComponent();
            connection.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source =C:\Users\Administrator\Desktop\EEE3111_EA.accdb; Persist Security Info = False;";
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "select * from EATable";
                //MessageBox.Show(query);
                command.CommandText = query;

                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    comboBox1.Items.Add(reader["FirstName"].ToString());
                    listBox1.Items.Add(reader["FirstName"].ToString());
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        private void SAVE_Click(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                command.CommandText = "insert into EEE3111_EA(FirstName,LastName,DOB,Country,Phone) values( '" + txt_FirstName + "' , '" + txt_LastName + "', '" + txt_DOB + "' , '" + txt_Country + "', '" + txt_Phone+ "')";
                command.ExecuteNonQuery();
                MessageBox.Show("Data Saved");
                connection.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        private void EDIT_Click(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query =  " FirstName='" + txt_FirstName.Text + "', LastName='" + txt_LastName.Text + "',DOB= '" + txt_DOB.Text + "',Country= '" + txt_Country.Text+ "',Phone = '" + txt_Phone.Text + ", EID='" + txt_EID.Text + "";

                MessageBox.Show(query);
                command.CommandText = query;

                command.ExecuteNonQuery();
                MessageBox.Show("Data Edit Successful");
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        private void DELETE_Click(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "delete from EATable where EID=" + txt_EID.Text + "";

                //MessageBox.Show(query);
                command.CommandText = query;

                command.ExecuteNonQuery();
                MessageBox.Show("Data Deleted");
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "select * from EATable where FirstName = '" +
                comboBox1.Text + "'";
                command.CommandText = query;

                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    txt_EID.Text = reader["EID"].ToString();
                    txt_FirstName.Text = reader["FirstName"].ToString();
                    txt_LastName.Text = reader["LastName"].ToString();
                    txt_DOB.Text = reader["DOB"].ToString();
                    txt_Country.Text = reader["Country"].ToString();
                    txt_Phone.Text = reader["Phone"].ToString();
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "select * from EATable where FirstName = '" +
                listBox1.Text + "'";
                command.CommandText = query;
                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    txt_EID.Text = reader["EID"].ToString();
                    txt_FirstName.Text = reader["FirstName"].ToString();
                    txt_LastName.Text = reader["LastName"].ToString();
                    txt_DOB.Text = reader["DOB"].ToString();
                    txt_Country.Text = reader["Country"].ToString();
                    txt_Phone.Text = reader["Phone"].ToString();
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }
    }
}

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-12-12 17:07
    关注

    报错的是76行的sql语句。更新的话要用update,自己注意天更加id的条件,如果是根据eid更新改成下面的。 where EID=" + txt_EID.Text

    string query =  "update EATable  set FirstName='" + txt_FirstName.Text + "', LastName='" + txt_LastName.Text + "',DOB= '" + txt_DOB.Text + "',Country= '" + txt_Country.Text+ "',Phone = '" + txt_Phone.Text + " where EID='" + txt_EID.Text + "'";
    

    注意如果eid数据库是数字用下面的

    string query =  "update EATable  set FirstName='" + txt_FirstName.Text + "', LastName='" + txt_LastName.Text + "',DOB= '" + txt_DOB.Text + "',Country= '" + txt_Country.Text+ "',Phone = '" + txt_Phone.Text + " where EID=" + txt_EID.Text ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 已采纳回答 12月13日
  • 创建了问题 12月12日

悬赏问题

  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据