C#对SQLite、Access数据库操作的封装,很好用的~
1、对SQLite的封装:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite; namespace DataBaseHelper
{
public class SQLiteHelper
{
SQLiteConnection connection = null;
SQLiteTransaction transaction = null;
string conn_str = ""; //----创建连接串并连接数据库----
public SQLiteHelper(string path, string password)
{
conn_str = "data source=" + path + ";password=" + password;
} public bool Connect()
{
try
{
if (connection != null)
{
connection.Close();
connection = null;
} connection = new SQLiteConnection(conn_str);
connection.Open();
if (connection == null)
{
return false;
}
return true;
}
catch (SQLiteException ex)
{
return false;
}
} //----修改数据库密码----
public bool ChangePassword(string newPassword)
{
try
{
connection.ChangePassword(newPassword);
return true;
}
catch (SQLiteException ex)
{
return false;
}
} //----关闭数据库连接----
public bool DisConnect()
{
try
{
if (connection != null)
{
connection.Close();
connection = null;
}
return true;
}
catch (SQLiteException ex)
{
return false;
}
} /// <summary>
/// 执行一个查询语句,返回一个包含查询结果的DataTable
/// </summary>
/// <param name="sql">要执行的查询语句</param>
/// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>
/// <returns></returns>
public DataTable ExecuteDataTable(string sql, SQLiteParameter[] parameters)
{
try
{
using (SQLiteCommand Command = new SQLiteCommand(sql, connection))
{
if (parameters != null)
{
Command.Parameters.AddRange(parameters);
}
SQLiteDataAdapter adapter = new SQLiteDataAdapter(Command);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
}
catch (SQLiteException ex)
{
return null;
}
} /// <summary>
/// 对SQLite数据库执行增删改操作,返回受影响的行数。
/// </summary>
/// <param name="sql">要执行的增删改的SQL语句</param>
/// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>
/// <returns></returns>
public int ExecuteNonQuery(string sql, SQLiteParameter[] parameters)
{
int affectRows = ; try
{
using (SQLiteTransaction Transaction = connection.BeginTransaction())
{
using (SQLiteCommand Command = new SQLiteCommand(sql, connection, Transaction))
{
if (parameters != null)
{
Command.Parameters.AddRange(parameters);
}
affectRows = Command.ExecuteNonQuery();
}
Transaction.Commit();
}
}
catch (SQLiteException ex)
{
affectRows = -;
}
return affectRows;
} //收缩数据库 VACUUM
public bool Vacuum()
{
try
{
using (SQLiteCommand Command = new SQLiteCommand("VACUUM", connection))
{
Command.ExecuteNonQuery();
}
return true;
}
catch (System.Data.SQLite.SQLiteException ex)
{
return false;
} } public void BeginTransaction()
{
try
{
transaction = connection.BeginTransaction();
}
catch (SQLiteException ex)
{ }
} public void CommitTransaction()
{
try
{
transaction.Commit();
}
catch (SQLiteException ex)
{ }
} public void RollbackTransaction()
{
try
{
transaction.Rollback();
}
catch (SQLiteException ex)
{ }
} public void test()
{
SQLiteHelper helper = new SQLiteHelper("D:\\mysqlite.db",""); //连接到D盘下的mysqlite.db数据库,连接密码为123456
//bool ch = helper.ChangePassword("654321"); //将密码修改为:654321
helper.Connect(); string select_sql = "select * from student"; //查询的SQL语句
DataTable dt = helper.ExecuteDataTable(select_sql, null); //执行查询操作,结果存放在dt中 //向数据库中student表中插入了一条(name = "马兆瑞",sex = "男",telephone = "15550000000")的记录
string insert_sql = "insert into student(name,sex,telephone) values(?,?,?)"; //插入的SQL语句(带参数)
SQLiteParameter[] para = new SQLiteParameter[]; //构造并绑定参数
string[] tag = { "name", "sex", "telephone" };
string[] value = { "马兆瑞","男",""};
for (int i = ; i < ; i++)
{
para[i] = new SQLiteParameter(tag[i], value[i]);
}
int ret = helper.ExecuteNonQuery(insert_sql, para); //执行插入操作
}
}
}
2、对Access的封装:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb; namespace DataBaseHelper
{
public class AccessHelper
{
OleDbConnection connection = null;
OleDbTransaction transaction = null;
string conn_str = ""; //----创建连接串并连接数据库----
public AccessHelper(string path, string password)
{
conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Jet OLEDB:Database Password= " + password;
} public bool Connect()
{
try
{
if (connection != null)
{
connection.Close();
connection = null;
} connection = new OleDbConnection(conn_str);
connection.Open();
if (connection == null)
{
return false;
}
return true;
}
catch (OleDbException ex)
{
return false;
}
} ////----修改数据库密码----
//public bool ChangePassword(string newPassword)
//{
// try
// {
// connection.ChangePassword(newPassword);
// return true;
// }
// catch (OleDbException ex)
// {
// return false;
// }
//} //----关闭数据库连接----
public bool DisConnect()
{
try
{
if (connection != null)
{
connection.Close();
connection = null;
}
return true;
}
catch (OleDbException ex)
{
return false;
}
} /// <summary>
/// 执行一个查询语句,返回一个包含查询结果的DataTable
/// </summary>
/// <param name="sql">要执行的查询语句</param>
/// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>
/// <returns></returns>
public DataTable ExecuteDataTable(string sql, OleDbParameter[] parameters)
{
try
{
using (OleDbCommand Command = new OleDbCommand(sql, connection))
{
if (parameters != null)
{
Command.Parameters.AddRange(parameters);
}
OleDbDataAdapter adapter = new OleDbDataAdapter(Command);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
}
catch (OleDbException ex)
{
return null;
}
} /// <summary>
/// 对Access数据库执行增删改操作,返回受影响的行数。
/// </summary>
/// <param name="sql">要执行的增删改的SQL语句</param>
/// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>
/// <returns></returns>
public int ExecuteNonQuery(string sql, OleDbParameter[] parameters)
{
int affectRows = ; try
{
using (OleDbTransaction Transaction = connection.BeginTransaction())
{
using (OleDbCommand Command = new OleDbCommand(sql, connection, Transaction))
{
if (parameters != null)
{
Command.Parameters.AddRange(parameters);
}
affectRows = Command.ExecuteNonQuery();
}
Transaction.Commit();
}
}
catch (OleDbException ex)
{
affectRows = -;
}
return affectRows;
} //收缩数据库 VACUUM
public bool Vacuum()
{
try
{
using (OleDbCommand Command = new OleDbCommand("VACUUM", connection))
{
Command.ExecuteNonQuery();
}
return true;
}
catch (OleDbException ex)
{
return false;
} } public void BeginTransaction()
{
try
{
transaction = connection.BeginTransaction();
}
catch (OleDbException ex)
{ }
} public void CommitTransaction()
{
try
{
transaction.Commit();
}
catch (OleDbException ex)
{ }
} public void RollbackTransaction()
{
try
{
transaction.Rollback();
}
catch (OleDbException ex)
{ }
}
}
}
调用示例:
AccessHelper helper = new AccessHelper("D:\\myaccess.mdb",""); //连接到D盘下的myaccess.mdb数据库,密码为123456789
helper.Connect();
string select_sql = "select * from student"; //查询的SQL语句
DataTable dt = helper.ExecuteDataTable(select_sql, null); //执行查询操作,结果存放在dt中
//向数据库中student表中插入了一条(name = "马兆瑞",sex = "男",telephone = "15550000000")的记录
string insert_sql = "insert into student(name,sex,telephone) values(?,?,?)"; //插入的SQL语句(带参数)
OleDbParameter[] para = new OleDbParameter[]; //构造并绑定参数
string[] tag = { "name", "sex", "telephone" };
string[] value = { "马兆瑞","男",""};
for (int i = ; i < ; i++)
{
para[i] = new OleDbParameter(tag[i], value[i]);
}
int ret = helper.ExecuteNonQuery(insert_sql, para); //执行插入操作
本人是IT菜鸟,代码有很多不足之处,望大家多多指教
C#对SQLite、Access数据库操作的封装,很好用的~的更多相关文章
- C# ACCESS数据库操作类
这个是针对ACCESS数据库操作的类,同样也是从SQLHELPER提取而来,分页程序的调用可以参考MSSQL那个类的调用,差不多的,只是提取所有记录的数量的时候有多一个参数,这个需要注意一下! usi ...
- PHP中对数据库操作的封装
在动态网面设计中很多都要涉及到对数据库的操作,但是有时跟据需要而改用其它后台数据库,就需要大量修改程序.这是一件枯燥.费时而且容易出错的功作.其实我们可以用PHP中的类来实现对数据库操作的封装,从而使 ...
- C# .NET更智能的数据库操作的封装
前述: 对数据库操作的封装,相信网络上已经有一大堆,ORM框架,或者是.NET本身的EF,都很好的支持数据库操作.这篇文章是分享自己所思考的,对数据库操作的简单封装.我对于这篇文章,认为被浏览者所关注 ...
- 手把手封装数据层之DataUtil数据库操作的封装
上一篇我们写完了数据库连接的封装 没有看的请移步上一篇关于数据库连接的内容 这次我们讲数据库操作的封装.数据库的操作就是增删改查:心再大一点就可以直接分为查询和其他. 因为查询是有返回对象的,而其他都 ...
- Microsoft Access数据库操作类(C#)
博文介绍的Microsoft Access数据库操作类是C#语言的,可实现对Microsoft Access数据库的增删改查询等操作.并且该操作类可实现对图片的存储,博文的最后附上如何将Image图片 ...
- C# 对Access数据库操作的通用类
(转载自博主Jerry很简单) //Access数据库-C# 操作类 代码using System;using System.Collections.Generic;using System.Linq ...
- C# .NET更智能的数据库操作的封装完整版(重构)
前述: 第一次发表文章,不过是对数据库简单的封装,主要是阐述下思路.那么在上篇文章,在大家的指导下和提出意见,并自己对代码进行了思考.在这两天我重构了新的框架,我觉得我写的可以称得上框架,为什么?请大 ...
- asp.net mvc access数据库操作
连接access数据库其实也简单,只要按照mvc的模式来就可以,遵循c v约定就可以 既然渲染试图是强类型,那么取得的数据就转换成强类型,其他一切和asp.net操作一样 DB mydb = new ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]
原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...
随机推荐
- 【广告】win10 uwp 水印图床 含代码
本文主要是广告我的软件. 图床可以加速大家写博客上传图片的时间,通过简化我们的操作来得到加速. 在写博客的时候,我们发现,我们需要上传一张图片,需要先打开图片,然后选择本地图片,然后上传. 但是我经常 ...
- substance在java swing中使用注意事项
package org.dgw.uidemo; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.UI ...
- 牛顿插值法及其C++实现
h1 { margin-bottom: 0.21cm } h1.western { font-family: "Liberation Sans", sans-serif; font ...
- Spring bean 生命周期验证
一.从源码注释看bean生命周期 从JDK源码上看,BeanFactory实现类需要支持Bean的完整生命周期,完整的初始化方法及其标准顺序(格式:接口 方法)为: 1.BeanNameAware s ...
- [mysql使用(2)] mysql的一些语法与Oracle的差别
一.表空间 mysql的表空间有共享表空间和独占表空间,独占表空间,其实就是一张表一个表空间,其实也就是一张表一个数据文件,共享表空间似乎有点类似oracle的表空间,不同的表可以保存在同一个数据文件 ...
- css实现的交互运动
<style type="text/css"> .filter-mix { position: absolute; top: 50%; left: 50%; trans ...
- 树莓派.GPRS.短信接收器
起因 曾经用过西门子出的短信猫, 好处是直接有SDK开发包, 不会硬件开发也能直接使用 缺点也是明显的, 就是只支持Windows系统, 另外就是在Windows下工作很不稳定, 隔开几天就会出现收不 ...
- 改变 font Awesome、Ionic 图标属性
可使用 -webkit-text-stroke属性. 例如: -webkit-text-stroke:1px red 使用color属性,可改变 图标颜色: 例如: color: red: 使用fon ...
- 67、django之模型层(model)--查询补充及mookie
本篇导航: F查询与Q查询 cookie 一.F查询与Q查询 1.以Book表为例 class Book(models.Model) : title = models.CharField(max_le ...
- 仿微信抢红包(js 转)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...