Home > Net >  Appear when delete operation is performed "System. The Data. The OleDb. OleDbException:" c
Appear when delete operation is performed "System. The Data. The OleDb. OleDbException:" c

Time:11-27

using System;
using System.Collections.Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;
Using System. Windows. Forms;
Using System. The Data. The OleDb;
The namespace database connection
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
}
Public String GetSqlStr (String STR)
{
Return STR. Replace (" '", ""'");
}
Public void ShowPerson ()
{
String connStr selectCmd;
ConnStr="Provider=Microsoft. Jet. The OLEDB. 4.0. The Data Source=PERSONDB. MDB ";
SelectCmd="Select * From the PERSON the Order By id_no DESC";
OleDbConnection conn.
OleDbDataAdapter myAdapter;
The DataSet myDataSet=new DataSet ();
Conn=new OleDbConnection (connStr);
Conn. The Open ();
MyAdapter=new OleDbDataAdapter (selectCmd, conn);
MyAdapter. The Fill (myDataSet, "PERSON");
DataGridView1. The DataSource=myDataSet. Tables (" PERSON ");
}

Private void Form1_Load (object sender, EventArgs e)
{
ShowPerson ();
}

Private void btnAdd_Click (object sender, EventArgs e)
{
String connStr insertCmd;
ConnStr="Provider=Microsoft. Jet. The OLEDB. 4.0. The Data Source=PERSONDB. MDB ";
InsertCmd="Insert Into PERSON (name, id_position, tel, salary) Values ('" + GetSqlStr (txtName. Text) + "', '" + GetSqlStr (txtPosition. Text) +"', '" + GetSqlStr (txtTel. Text) + ""," + int. Parse (txtSalary. Text) + ") ";
OleDbConnection conn.
OleDbCommand CMD.
Conn=new OleDbConnection (connStr);
Conn. The Open ();
CMD=new OleDbCommand (insertCmd, conn);
CMD. ExecuteNonQuery ();
Conn. Close ();
ShowPerson ();
}
Private void btnUpdate_Click (object sender, EventArgs e)
{
String connStr updateCmd;
ConnStr="Provider=Microsoft. Jet. The OLEDB. 4.0. The Data Source=PERSONDB. MDB ";
UpdateCmd="update PERSON Set id_position='" + GetSqlStr (txtPosition. Text) +"', tel='" + GetSqlStr (txtTel. Text) + "', salary="+ int. Parse (txtSalary. Text) +" Where name='" + GetSqlStr (txtName. Text) + "' ";
OleDbConnection conn.
OleDbCommand CMD.
Conn=new OleDbConnection (connStr);
Conn. The Open ();
CMD=new OleDbCommand (updateCmd, conn);
CMD. ExecuteNonQuery ();
Conn. Close ();
ShowPerson ();
}

Private void btndel_Click (object sender, EventArgs e)
{
String connStr delCmd;
ConnStr="Provider=Microsoft. Jet. The OLEDB. 4.0. Data Sourse=PERSONDB. MDB ";
DelCmd="Delete From the PERSON Where name='" + GetSqlStr (txtName. Text) +"' ";
OleDbConnection conn.
OleDbCommand CMD.
Conn=new OleDbConnection (connStr);
Conn. The Open ();
CMD=new OleDbCommand (delCmd, conn);
CMD. ExecuteNonQuery ();
Conn. Close ();
ShowPerson ();
}
}
}
  •  Tags:  
  • C#
  • Related