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日

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗