C#操作Access的查询、添加、删除、修改源程序.doc
文本预览下载声明
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Data.OleDb;
using System.Text;
using System.Windows.Forms;
namespace Location
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = Location System;
button1.Text = 连接数据库;
button2.Text = 查询;
button3.Text = 退出;
button4.Text = 添加;
button5.Text = 删除;
button6.Text = 修改;
label1.Text = ID:;
textBox1.Text = 0;
}
private void button1_Click(object sender, EventArgs e)
{
string ConStr = Provider=Microsoft.Jet.OLEDB.4.0;data source=Location.mdb;//创建OleDbConnection对象
OleDbConnection con = new OleDbConnection(ConStr);
con.Open();
if (con.State == ConnectionState.Open)
{
MessageBox.Show(Access数据库的连接成功!, Access数据库的连接);
}
else
{
MessageBox.Show(Access数据库的连接失败!, Access数据库的连接);
}
con.Close();
}
private void button3_Click(object sender, EventArgs e) //退出
{
this.Close();
}
private void button2_Click(object sender, EventArgs e) //查询模块
{
string ConStr = Provider=Microsoft.Jet.OLEDB.4.0;data source=Location.mdb;//创建OleDbConnection对象
OleDbConnection con = new OleDbConnection(ConStr);
con.Open();
int i = Convert.ToInt16(textBox1 .Text);
OleDbCommand cmd = new OleDbCommand(Select * From data where ID=@id, con);
cmd.Parameters.Add(@id,i);
OleDbDataReader reader = cmd.ExecuteReader();
reader.Read();
显示全部