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();
}
}
}
}
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();
}
}
}
}
相关文章推荐
- ASP.NET C#各种数据库连接字符串大全——SQLServer、Oracle、Access
- ASP.NET C#各种数据库连接字符串大全——SQLServer、Oracle、Access
- C#(ASP.NET)中连接数据库的字符串(连接Access和sqlserver数据库以及用windows和身份验证两种方式)
- c#.net连接access操作类 (转)|| C#中如何连接Access2007数据库
- 【商业版】C# ASP.NET 通用权限管理系统组件源码中的数据库访问组件可以全面支持Access单机数据库了
- C# 连接 Access 数据库
- Asp.net C# 连接access 的办法
- NET(C#)连接各类数据库-集锦(转)
- C# SQL和Access连接数据库Helper类,很爽很强大!!!
- C#连接4种类型数据库(Access、SQL Server、Oracle、MySQL)
- C#连接一些常见的数据库(sql,Access,Oracle,Sybase,DB2)
- 用ASP.NET和C#连接数据库的几种方法
- C#使用ADO.net连接数据库
- c#.net连接access操作类
- ADO.NET 连接数据库字符串(Oracle、SqlServer、Access、ODBC)
- C#连接数据库--VS中使用MYSQL connect Net 连接本地MYSQL
- asp.net c# 连接各种数据库代码
- C# ADO.NET的五个常用对象及数据库连接操作
- NET(C#)连接各类数据库-集锦 --【C#网文收集系列】.
- 在ASP.NET(C#)中使用Oralce Object For OLE 2.3连接Oracle 8.0.5数据库!