您的位置:首页 > 数据库

C#.NET 数据库连接(Access)

2008-11-29 11:18 513 查看
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 lianjie

{

public partial class Form1 : Form

{

OleDbConnection strCon = new OleDbConnection();

OleDbDataAdapter strDA;

OleDbCommandBuilder strCB;

DataTable strDT = new DataTable();

int m_row = 0;

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

strCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= E:\Database1.mdb ";

strCon.Open();

strDA = new OleDbDataAdapter("Select * from LIANXI", strCon);

strCB = new OleDbCommandBuilder(strDA);

strDA.Fill(strDT);

this.showCurrentRecord();

}

private void showCurrentRecord()//显示纪录

{

if (strDT.Rows.Count == 0)

{

textBox1.Text = "000";

textBox2.Text="NO";

return;

}

textBox1.Text = strDT.Rows[m_row]["ID"].ToString();

textBox2.Text=strDT.Rows[m_row]["name"].ToString();

}

private void button1_Click(object sender, EventArgs e)//第一条记录

{

m_row = 0;

this.showCurrentRecord();

}

private void button2_Click(object sender, EventArgs e)//下一条

{

if (m_row<strDT.Rows.Count-1)

{

m_row++;

this.showCurrentRecord();

}

}

private void button3_Click(object sender, EventArgs e)//上一条

{

if (m_row != 0)

{

m_row--;

this.showCurrentRecord();

}

}

private void button4_Click(object sender, EventArgs e)//最后一条

{

if (strDT.Rows.Count != 0)

{

m_row = strDT.Rows.Count - 1;

this.showCurrentRecord();

}

}

private void button5_Click(object sender, EventArgs e)//ADD

{

DataRow strRow = strDT.NewRow();

strRow["ID"] = textBox4.Text;

strRow["name"] = textBox3.Text;

strDT.Rows.Add(strRow);

strDA.Update(strDT);

m_row = strDT.Rows.Count - 1;

this.showCurrentRecord();

}

private void button6_Click(object sender, EventArgs e)//Delete

{

if (strDT.Rows.Count != 0)

{

strDT.Rows[m_row].Delete();

strDA.Update(strDT);

m_row = 0;

this.showCurrentRecord();

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: