1先在access中建立一个数据库名称为“user.mdb”,在数据库中建立表名为“user1”的表
2.form.cs代码如下:
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 OleDbConnection Aconnstr;
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
//连接数据库
private void button1_Click(object sender, EventArgs e)
{
string Afile = "provider = Microsoft.Jet.OLEDB.4.0; Data Source = user1.mdb";
Aconnstr = new OleDbConnection(Afile);
}
//检索数据
private void button2_Click(object sender, EventArgs e)
{
Aconnstr.Open();
// textBox1.AppendText("jkjfklj");
OleDbCommand Acmd = new OleDbCommand("select * from user1;", Aconnstr);
OleDbDataReader odr = null;
try
{
//listBox1.Items.Add("ID号\t用户名");
odr = Acmd.ExecuteReader();
}
catch (Exception ex)
{
if(ex == null)
MessageBox.Show("执行出错");
}
//
if(odr != null)
{
//textBox1.AppendText("jkjfklj");
listBox1.Items.Add("ID号\t用户名");
while(odr.Read())
{
string totalinfo = "";
totalinfo += odr["ID号"].ToString() + "\t";
totalinfo += odr["用户名"].ToString() + "\n";
listBox1.Items.Add(totalinfo + "\n");
}
odr.Close();
}
Aconnstr.Close();
}
//插入数据
private void button3_Click(object sender, EventArgs e)
{
OleDbCommand icomd = new OleDbCommand("insert into user1(ID号,用户名) values('1','newperson');", Aconnstr);
Aconnstr.Open();
try
{
icomd.ExecuteNonQuery();
}
catch (Exception ex)
{
if (ex != null)
MessageBox.Show("插入操作错误");
}
MessageBox.Show("插入成功!");
Aconnstr.Close();
}
//删除数据
private void button4_Click(object sender, EventArgs e)
{
OleDbCommand dcmd = new OleDbCommand("delete from user1 where ID号 = 1;", Aconnstr);
Aconnstr.Open();
dcmd.ExecuteNonQuery();
MessageBox.Show("删除成功!");
Aconnstr.Close();
}
//修改数据
private void button5_Click(object sender, EventArgs e)
{
OleDbCommand dcmd = new OleDbCommand("update user1 set 用户名 = 'test' where ID号=1;", Aconnstr);
Aconnstr.Open();
dcmd.ExecuteNonQuery();
MessageBox.Show("更新成功!");
Aconnstr.Close();
}
//更新数据
private void button6_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
this.button2_Click(sender, e);
}
}
}
designer的代码为:
namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 34);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(122, 38);
this.button1.TabIndex = 0;
this.button1.Text = "建立数据库连接";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(398, 75);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "检索数据";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(398, 134);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "插入数据";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(398, 206);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 3;
this.button4.Text = "删除数据";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(398, 281);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 4;
this.button5.Text = "修改数据";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(398, 366);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 5;
this.button6.Text = "刷新数据";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(14, 102);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(328, 316);
this.listBox1.TabIndex = 6;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(555, 453);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.ListBox listBox1;
}
}