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);
}
}
}
}
报错的是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 ;
76行的sql语句不对