C#操作Access的查询、添加、删除、修改源程序
C#操作Access的查询、添加、删除、修改源程序
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 = "";
} 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(); //textBox1.Text = reader[0].ToString();
textBox2.Text = reader[].ToString();
textBox3.Text = reader[].ToString();
textBox4.Text = reader[].ToString();
textBox5.Text = reader[].ToString();
textBox6.Text = reader[].ToString();
textBox7.Text = reader[].ToString(); reader.Close();
con.Close();
} private void button4_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(); for (int i = ; i < ; i++)
{
string sql = "insert into data(ID)values(" + i + ")";
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.ExecuteNonQuery();
} con.Close();
} private void button5_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();
OleDbCommand cmd = new OleDbCommand("delete from data", con);
cmd.ExecuteNonQuery();
} private void button6_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();
string sql = "update data set longitude=12 where ID=1";
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.ExecuteNonQuery();
}
}
}
C#操作Access类
using System;
using System.Data;
using System.Data.OleDb; namespace AccessDb
{
/**//// <summary>
/// AccessDb 的摘要说明,以下信息请完整保留
/// 请在数据传递完毕后调用Close()方法,关闭数据链接。
/// </summary>
public class AccessDbClass
{ //变量声明处#region 变量声明处
public OleDbConnection Conn;
public string ConnString;//连接字符串
#endregion //构造函数与连接关闭数据库#region 构造函数与连接关闭数据库
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="Dbpath">ACCESS数据库路径</param>
public AccessDbClass(string Dbpath)
{
ConnString ="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
ConnString += Dbpath;
Conn =new OleDbConnection(ConnString);
Conn.Open();
} /**//// <summary>
/// 打开数据源链接
/// </summary>
/// <returns></returns>
public OleDbConnection DbConn()
{
Conn.Open();
return Conn;
} /**//// <summary>
/// 请在数据传递完毕后调用该函数,关闭数据链接。
/// </summary>
public void Close()
{
Conn.Close();
}
#endregion //数据库基本操作
#region 数据库基本操作
/**//// <summary>
/// 根据SQL命令返回数据DataTable数据表,
/// 可直接作为dataGridView的数据源
/// </summary>
/// <param name="SQL"></param>
/// <returns></returns>
public DataTable SelectToDataTable(string SQL)
{
OleDbDataAdapter adapter =new OleDbDataAdapter();
OleDbCommand command =new OleDbCommand(SQL, Conn);
adapter.SelectCommand = command;
DataTable Dt =new DataTable();
adapter.Fill(Dt);
return Dt;
} /**//// <summary>
/// 根据SQL命令返回数据DataSet数据集,其中的表可直接作为dataGridView的数据源。
/// </summary>
/// <param name="SQL"></param>
/// <param name="subtableName">在返回的数据集中所添加的表的名称</param>
/// <returns></returns>
public DataSet SelectToDataSet(string SQL,string subtableName)
{
OleDbDataAdapter adapter =new OleDbDataAdapter();
OleDbCommand command =new OleDbCommand(SQL, Conn);
adapter.SelectCommand = command;
DataSet Ds =new DataSet();
Ds.Tables.Add(subtableName);
adapter.Fill(Ds, subtableName);
return Ds;
} /**//// <summary>
/// 在指定的数据集中添加带有指定名称的表,由于存在覆盖已有名称表的危险,返回操作之前的数据集。
/// </summary>
/// <param name="SQL"></param>
/// <param name="subtableName">添加的表名</param>
/// <param name="DataSetName">被添加的数据集名</param>
/// <returns></returns>
public DataSet SelectToDataSet (string SQL,string subtableName, DataSet DataSetName)
{
OleDbDataAdapter adapter =new OleDbDataAdapter();
OleDbCommand command =new OleDbCommand(SQL, Conn);
adapter.SelectCommand = command;
DataTable Dt =new DataTable();
DataSet Ds =new DataSet();
Ds = DataSetName;
adapter.Fill(DataSetName, subtableName);
return Ds;
} /**//// <summary>
/// 根据SQL命令返回OleDbDataAdapter,
/// 使用前请在主程序中添加命名空间System.Data.OleDb
/// </summary>
/// <param name="SQL"></param>
/// <returns></returns>
public OleDbDataAdapter SelectToOleDbDataAdapter(string SQL)
{
OleDbDataAdapter adapter =new OleDbDataAdapter();
OleDbCommand command =new OleDbCommand(SQL, Conn);
adapter.SelectCommand = command;
return adapter;
} /**//// <summary>
/// 执行SQL命令,不需要返回数据的修改,删除可以使用本函数
/// </summary>
/// <param name="SQL"></param>
/// <returns></returns>
public bool ExecuteSQLNonquery(string SQL)
{
OleDbCommand cmd =new OleDbCommand(SQL, Conn);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}
#endregion
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/stxyc/archive/2010/04/19/5501232.aspx
使用例子:
using AccessDb;
...
//初始化,载入数据库路径
AccessDbClass mydb = new AccessDbClass("c:/db.mdb"); //返回符合SQL要求的DataTable,并且与控件dataGridView1绑定
DataTable dt = new DataTable();
dt = mydb.SelectToDataTable(@"select * from student");
this.dataGridView1.DataSource = dt; //返回DataSet,其中包括一个符合SQL要求和给定名称的DataTable,并且与控件dataGridView1绑定
DataSet ds = new DataSet();
ds = mydb.SelectToDataSet(@"select * from student","student");
this.dataGridView1.DataSource = ds.Tables["student"]; //关闭数据库
mydb.Close();
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/stxyc/archive/2010/04/19/5501232.aspx
c#.NET实现数据读写Access
.写入数据库
---------------------------------
using System.Data.OleDb;
using System.IO;
---------------------------------
//ACCESS数据库的连接字符串
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=";
strConn += Server.MapPath(".\\Data\\accDB.mdb");
//生成一个新的连接
string strCom = "Insert into jieguo (RESULT,GRAND,imgURL) Values ('"
+ TextBox1.Text.ToString() + "','"
+ TextBox2.Text.ToString() + "','"
+ "~//houseImg//" + FileUpload1.FileName + "')";
OleDbConnection myConn = new OleDbConnection(strConn);
myConn.Open();
OleDbCommand oldConn = new OleDbCommand(strCom, myConn);
oldConn.ExecuteNonQuery();
myConn.Close();
Response.Redirect("Default.aspx"); .读数据库(以登录验证为例)
---------------------------------
using System.Data.OleDb;
using System.IO;
---------------------------------
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=";
strConn += Server.MapPath(".\\Data\\accDB.mdb");
OleDbConnection myConn = new OleDbConnection(strConn);
//定义数据适配器
OleDbDataAdapter oda = new OleDbDataAdapter();
string strsql;
strsql = "select * from users where name='" + AdminName.Text.ToString() + "'and pass='" + AdminPwd.Text.ToString() + "'";
DataSet dsMsg = new DataSet();
oda.SelectCommand = new OleDbCommand(strsql, myConn);
oda.Fill(dsMsg);
if (dsMsg.Tables[].Rows.Count == )
Response.Write("<script>alert(\"用户名不存在\");</script>");
else
Response.Redirect("next.aspx");
using System;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private string strCnn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\c\WindowsFormsApplication7\aspxWeb.mdb;Persist Security Info=True";
private void button2_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = this.openFileDialog1.FileName;
}
} private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Equals(String.Empty))
{
MessageBox.Show("先选择文件。");
return;
} FileStream fs = new FileStream(this.textBox1.Text, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] buffer = br.ReadBytes((int)fs.Length);
OleDbConnection myConnection = new OleDbConnection(strCnn);
OleDbCommand command = new OleDbCommand("INSERT INTO TestTable (Title,Description,nr)" +
"VALUES (@Title,@Description,@nr)", myConnection); command.Parameters.AddWithValue("@Title", "a");
command.Parameters.AddWithValue("@Description", "mengxianhui@dotnet.aspx.cc");
command.Parameters.AddWithValue("@nr", buffer); //打开连接,执行查询
myConnection.Open();
command.ExecuteNonQuery();
myConnection.Close();
br.Close();
fs.Close();
MessageBox.Show("保存完毕。");
} private void button3_Click(object sender, EventArgs e)
{
//构建数据库连接,SQL语句,创建参数
OleDbConnection myConnection = new OleDbConnection(strCnn);
myConnection.Open();
OleDbCommand command = new OleDbCommand("select top 1 * from TestTable Order By id DESC", myConnection);
OleDbDataReader dr = command.ExecuteReader();
byte[] buff = null;
if (dr.Read())
{
buff = (byte[])dr["nr"];
} String p = Application.ExecutablePath;
p = p.Substring(,p.LastIndexOf("\\"));
p += "\\m.doc";
this.textBox2.Text = p ;
if (File.Exists(p)) File.Delete(p);
myConnection.Close();
System.IO.FileStream stream = new System.IO.FileStream(p, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(stream);
bw.Write(buff);
bw.Close();
stream.Close();
MessageBox.Show("生成完毕。");
}
}
}
C#操作Access的查询、添加、删除、修改源程序的更多相关文章
- Dom4j 操作, 节点查找 添加 删除 修改 。。。xPath
转: Dom4j 操作, 节点查找 添加 删除 修改 ...xPath 2013年11月28日 10:48:59 今晚打酱油8 阅读数:8506更多 个人分类: JavaWeb 版权声明:本文为博 ...
- JavaScript学习 - 基础(八) - DOM 节点 添加/删除/修改/属性值操作
html代码: <!--添加/删除/修改 --> <div id="a1"> <button id="a2" onclick=&q ...
- dir(dict)|字典的创建-添加-删除-修改-判断存在-取值等相关操作
dir(dict) ####字典操作:创建-添加-删除-修改-判断存在-取值 #(一)创建字典: {} .等号. zip(). [(),()] #1.创建空字典 dict0 = {} #2.等号创建 ...
- SQL语句添加删除修改字段及一些表与字段的基本操作
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200)2.删除字段 ALTER TABLE table_NA ...
- 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP CO ...
- SQL语句添加删除修改字段[sql server 2000/2005]
用SQL语句添加删除修改字段1.增加字段 alter table docdsp add dspcodechar(200)2.删除字段 ALTER TABLE table_NAME ...
- SQL语句添加删除修改字段
用SQL语句添加删除修改字段1.增加字段 alter table docdsp add dspcodechar(200)2.删除字段 ALTER TABLE table_NAME ...
- JTree 添加 , 删除, 修改
package com.swing.demo; import java.awt.BorderLayout; import java.awt.Container; import java.awt.eve ...
- jQuery---jq操作标签文本(html(),text()),jq操作文档标签(插入,删除,修改),克隆,,jq操作属性,jq操作class属性,jq操作表单value,jq操作css,jq操作盒子(重要),jq操作滚动条
jQuery---jq操作标签文本(html(),text()),jq操作文档标签(插入,删除,修改),克隆,,jq操作属性,jq操作class属性,jq操作表单value,jq操作css,jq操作盒 ...
随机推荐
- 【Visual Studio】 使用EF、 Linq2Sql快速创建数据交互层(一)
项目伊始,创建数据库交互层代码是底层框架的首要任务.常用的做法包括手动编码.Hibernate或者动软之类的代码生成器,而多数人忽略了.Net环境下VS提供的两套非常好用的数据层工具. EF和Linq ...
- CentOS7 - 安装 MariaDB
1 安装并启动 MariaDB MariaDB 采用 Percona 的 XtraDB 存储引擎替代 MySQL 的 InnoDB,XtraDB 完全兼容 InnoDB. 1.1 安装 MySQL 与 ...
- (appium+python)UI自动化_08_unittest编写测试用例
前言 unittest是python自带的单元测试框架,类似于Junit(Java单元测试框架).支持自动化测试,可编写测试前置&后置条件,并且可批量运行测试用例并生成测试报告. 使用unit ...
- K-th Number 【POJ - 2104】【可持久化线段树】
题目链接 因为这道题没有删除修改之类的,所以很多人会用离散化之后的线段树来做,但是实际上(可能是我懒得去做离散化这个操作了),然后就是直接写可持久化线段树,区间的长度就是int的从最小到最大的长度,然 ...
- 网络流强化-HDU2732
第一次遇到加了“多余”的边会导致WA的——在我看来是很多余,见代码191行 之后会思考为什么,想出来再更. 问题弄明白了,如果你在连接边连了一条到没有柱子的点的边,这个没有柱子的点是不可能连到终点的, ...
- Scratch可视化的编程工具
1.是什么? 在线编程网址 是一个编程软件,但是不涉及编程语言,是针对青少年开发的编程积木模块. 2.软件的目的是什么? 激发青少年对编程的兴趣 3.怎么用呢? 软件内部是很多积木模块,需要明白每块是 ...
- 腾达Tenda W311MA无线网卡Linux下驱动安装
菜鸟看这里https://help.ubuntu.com/community/WifiD ... enda_W311M 最近也买了颗Tenda W311M网卡,简单说一下驱动的安装和hostapd做S ...
- VulnStack靶场实战(未完成)
环境搭建 https://www.cnblogs.com/HKCZ/p/11760213.html 信息收集 目录爆破 这里发现有phpmyadmin目录,这里可以直接获取webshell 参照: h ...
- mybatis插件机制及分页插件原理
MyBatis 插件原理与自定义插件: MyBatis 通过提供插件机制,让我们可以根据自己的需要去增强MyBatis 的功能.需要注意的是,如果没有完全理解MyBatis 的运行原理和插件的工作方式 ...
- SpringMvc和Mybatis整合需要配置的xml
applicationContext-dao.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...