使用oledb对数据库进行增删改查及批量插入操作

使用oledb操作数据库工具类,可使用泛型统一操作

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.OleDb;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Windows.Forms;

namespace CommonUtil

{

public class DataBaseUtil

{

//传递数据库文件路径,这里使用的是access2007数据库

public DataBaseUtil(string path)

{

Path = path;

ConnStr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False; ",

path);

conn = new OleDbConnection(ConnStr);

}

public string Path;

public static  string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\numdb.accdb;Persist Security Info=False; ";

private OleDbConnection conn= new OleDbConnection(ConnStr);//创建一个connection对象

//使用泛型,获取所有的实体类对象,返回list

public List<T> ReieveList<T>() where T : class,new()

{

try

{

var className = (typeof(T)).Name;

var sql = string.Format("SELECT *  FROM {0}", className);

OleDbDataAdapter da = new OleDbDataAdapter(sql, ConnStr);

DataSet ds = new DataSet();

da.Fill(ds);

var dt = ds.Tables[0];

var list = ConverterUtil.ConvertDataTableToList<T>(dt);

return list;

}

catch (Exception e)

{

MessageBox.Show(e.Message);

return null;

}

}

//同上,根据条件,查询,返回list实体类列表

public List<T> ReieveList<T>(string where) where T : class,new()

{

try

{

var className = (typeof(T)).Name;

var sql = string.Format("SELECT *  FROM {0} {1}", className,where);

OleDbDataAdapter da = new OleDbDataAdapter(sql, ConnStr);

DataSet ds = new DataSet();

da.Fill(ds);

var dt = ds.Tables[0];

var list = ConverterUtil.ConvertDataTableToList<T>(dt);

return list;

}

catch (Exception e)

{

MessageBox.Show(e.Message);

return null;

}

}

//插入一条数据

public bool Insert<T>(T entity) where T : class,new()

{

try

{

var type = typeof (T);

var className = type.Name;

var fields = "";

var values = "";

foreach (var property in type.GetProperties())

{

if (property.Name.Equals("ID")) continue;

fields += "," + property.Name;

var isNumStr = (property.PropertyType == typeof (double) ||

property.PropertyType == typeof (int))

? ""

: "‘";

values += "," + isNumStr + property.GetValue(entity, null) + isNumStr;

}

fields = fields.Substring(1);

values = values.Substring(1);

var sql = string.Format("insert into {0}({1}) values ({2}) ", className,

fields, values);

OleDbDataAdapter da = new OleDbDataAdapter();

da.InsertCommand = new OleDbCommand(sql, conn);

da.InsertCommand.CommandText = sql;

conn.Open();

da.InsertCommand.ExecuteNonQuery();

conn.Close();

return true;

}

catch (Exception e)

{

MessageBox.Show(e.Message);

return false;

}

finally

{

conn.Close();

}

}

//更新实体类

public bool Update<T>(T entity) where T : class,new()

{

try

{

var type = typeof(T);

var className = type.Name;

var values = "";

var id = "";

foreach (var property in type.GetProperties())

{

if (property.Name.Equals("ID"))

{

id = " where ID="+  property.GetValue(entity, null).ToString();

continue;

}

var isNumStr = (property.PropertyType == typeof(double) ||

property.PropertyType == typeof(int))

? ""

: "‘";

values += "," +property.Name +"="+ isNumStr + property.GetValue(entity, null) + isNumStr;

}

values = values.Substring(1);

var sql = string.Format("update {0} set {1} {2}", className,

values,id);

OleDbDataAdapter da = new OleDbDataAdapter();

da.UpdateCommand = new OleDbCommand(sql, conn);

da.UpdateCommand.CommandText = sql;

conn.Open();

da.UpdateCommand.ExecuteNonQuery();

conn.Close();

return true;

}

catch (Exception e)

{

MessageBox.Show(e.Message);

return false;

}

finally

{

conn.Close();

}

}

//根据条件删除数据

public bool Delete<T>(string  where)

{

try

{

var type = typeof(T);

var className = type.Name;

var sql = string.Format("delete from {0} {1}", className,

where);

OleDbDataAdapter da = new OleDbDataAdapter();

da.DeleteCommand = new OleDbCommand(sql, conn);

da.DeleteCommand.CommandText = sql;

conn.Open();

da.DeleteCommand.ExecuteNonQuery();

conn.Close();

return true;

}

catch (Exception e)

{

MessageBox.Show(e.Message);

return false;

}

finally

{

conn.Close();

}

}

//批量插入数据

public bool InsertList<T>(List<T> entitysList) where T : class,new()

{

try

{

var type = typeof (T);

var className = type.Name;

var fields = "";

var values = "";

foreach (var property in type.GetProperties())

{

if (property.Name.Equals("ID")) continue;

fields += "," + property.Name;

var isNumStr = (property.PropertyType == typeof (double) ||

property.PropertyType == typeof (int))

? ""

: "‘";

values += ",?" ;

}

fields = fields.Substring(1);

values = values.Substring(1);

var sql = string.Format("insert into {0}({1}) values ({2}) ", className,

fields, values);

OleDbDataAdapter da = new OleDbDataAdapter();

da.InsertCommand = new OleDbCommand(sql, conn);

da.InsertCommand.CommandText = sql;

foreach (var property in type.GetProperties())

{

if (property.Name.Equals("ID")) continue;

var oleType = (property.PropertyType == typeof(double) ||

property.PropertyType == typeof(int))

? OleDbType.Integer

: OleDbType.VarChar;

da.InsertCommand.Parameters.Add(property.Name, oleType, int.MaxValue,

property.Name);

fields += "," + property.Name;

values += ",?";

}

var table = ConverterUtil.ConvertListToDataTable(entitysList);

table.TableName = className;

da.Update(table);

return true;

}

catch (Exception e)

{

MessageBox.Show(e.Message);

return false;

}

finally

{

conn.Close();

}

}

//这个方法是用来执行无返回结果的插入语句,如 insert  select

public bool ExecuteInsertSql(string sql)

{

try

{

OleDbDataAdapter da = new OleDbDataAdapter();

da.InsertCommand = new OleDbCommand(sql, conn);

da.InsertCommand.CommandText = sql;

conn.Open();

da.InsertCommand.ExecuteNonQuery();

conn.Close();

return true;

}

catch (Exception e)

{

MessageBox.Show(e.Message);

return false;

}

finally

{

conn.Close();

}

}

}

}



时间: 2024-07-31 23:22:33

使用oledb对数据库进行增删改查及批量插入操作的相关文章

【Visual Basic】vb6的ListView控件,对Access2003数据库的增删改查,判断是否有中文、多窗体操作

vb6对Access2003数据库的增删改查并不复杂,可以通过ado对象轻松完成,下面举个小例子,同时说明vb6中的ListView控件的使用.虽然在<[Visual Basic]列表控件ListView的增删改查.模态对话框.禁止窗口调整大小>曾经对VB.NET的ListView控件进行详细的说明,但是证明微软就是个坑爹货,vb6对于ListView实现的代码居然跟VB.NET有着彻底的不同,似乎换了一门语言似得的.改代码什么的最讨厌的. 首先,在vb6生成的工程文件夹中有着一个db1.md

【Hibernate】Hibernate的在Eclipse+Mysql的配置、安装,纯Java,利用Annotation与HQL完成数据库的增删改查

这篇文章有很多槽点,在Hibernate4.x上面用着Hibernate3.x的写法去写.而且程序中放到Eclipse中会有一大堆警告,但是这好歹也在一定程度上完成了Hibernate的入门.毕竟现在很多介绍Hibernate的书籍都是用Hibernate3.x的写法去写.上次那篇<[Hibernate]最简单的Hibernate工程--账号注册系统>(点击打开链接)杂糅Struts的技术其实是不对的.因为Hibernate完成的是,从Java到数据库,从数据库到Java的任务.之后Java与

android中SQLite数据库的增删改查

1.数据库帮助类PersonSQLiteOpenHelper package com.wzw.sqllitedemo.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper

Windows下安装MySQLdb, Python操作MySQL数据库的增删改查

这里的前提是windows上已经安装了MySQL数据库,且配置完毕,能正常建表能操作.在此基础上只需安装MySQL-python-1.2.4b4.win32-py2.7.exe就ok了,只有1M多.这个有点类似jdbc里的那个jar包. 下载链接:http://sourceforge.net/projects/mysql-python/ , 百度云盘 :http://pan.baidu.com/s/1dDgnfpR 密码:7bna 接着import MySQLdb就能使用了,下面给出测试代码:

TP框架中 数据库的增删改查

框架会用到数据库的内容,这一篇就是关于数据库的增删改查. 数据库的操作,无疑就是连接数据库,然后对数据库中的表进行各种查询,然后就是对数据的增删改的操作, 想要操作数据库,第一步必然是要:链接数据库 一.链接数据库 (1)找到模块文件夹中的Conf文件夹,然后进行编写config.php文件 我这里是这样的文件路径 (2)打开这个config.php文件,然后找到父类配置文件convention.php文件,将关于"数据库"的部分复制粘贴到config.php配置文件中(父类的conv

mysql笔记--数据库基本增删改查 修改表结构

数据库基本增删改查 1. 增-添加/插入数据,insert into 插入哪张表,那些列,什么值, 语句:insert into 表名(列1,列2,列3)values (值1,值2,值3): 可以不按原列的顺序插入,也可以插入部分列,但是值与列要一一对应,不能混乱!!! 一次插入多行数据 : Insert into 表名(列1,列2)values (值1,值2),(值1,值2): 2. 改-更新数据update 更新哪张表,哪些列,哪些值 语句:update 表名 set 列1=值1,列2=值2

java jdbc 连接mysql数据库 实现增删改查

好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打交道非常基础的一个知识,也是比较接近底层的,在实际的工作中大家用得更多的其实还是比较成熟的框架,例如Hibernate.Mybatis. 但是作为这些成熟框架的底层的jdbc却也是我们应该去掌握的,只有了解了jdbc的增删改查,这样在以后如果有兴趣去研究Hibernate或者Mybatis的源代码的

Android学习---SQLite数据库的增删改查和事务(transaction)调用

上一篇文章中介绍了手工拼写sql语句进行数据库的CRUD操作,本文将介绍调用sqlite内置的方法实现CRUD操作,其实质也是通过拼写sql语句. 首先,创建一个新的android项目: 其次,查看代码实现增删查改: 1.创建DB工具类 MyDBHelper.java(创建数据库的操作) package com.amos.android_db; import android.content.Context; import android.database.sqlite.SQLiteDatabas

java:JSP(JSPWeb.xml的配置,动态和静态导入JSP文件,重定项和请求转发,使用JSP实现数据库的增删改查实例)

1.JSP的配置: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":&qu