楼主回复我一下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
{
private string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\fxf\database\Mydatabase.mdb";
private OleDbConnection conn=null;
private OleDbDataAdapter DataAdapter = null;
private DataSet dataset = null;
private string curNo = "";
public Form1()
{
InitializeComponent();
}
private void showData3() //在控件dataGridView1显示数据
{
try
{
if (conn == null) conn.Open();
DataAdapter = new OleDbDataAdapter("SELECT * FROM student", conn);
dataset = new DataSet();
DataAdapter.Fill(dataset, "t1");
dataGridView1.DataSource = dataset;
dataGridView1.DataMember = "t1";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (conn != null) conn.Close();
DataAdapter.Dispose();
dataset.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count <= 1) return;
int index = dataGridView1.CurrentRow.Index; //获取当记录的索引号
dataGridView1.Rows[index].Selected = true; //加亮显示
curNo = this.dataGridView1.Rows[index].Cells[0].Value.ToString();
OleDbCommand command = null;
string strSQL = "Delete From student Where 学号 = *" + curNo + "*";
try
{
command = new OleDbCommand();
command.Connection = conn;
command.CommandText = strSQL;
conn.Open();
int n = command.ExecuteNonQuery(); //执行Delete 语句
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (conn != null) conn.Close();
command.Dispose();
}
showData3();
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new OleDbConnection(constr);
showData3();
}
}
}