设为首页 加入收藏

TOP

使用oledb对数据库进行增删改查及批量插入操作(一)
2015-11-21 01:40:12 来源: 作者: 【 】 浏览:0
Tags:使用 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 ReieveList() 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(dt);

return list;
}
catch (Exception e)
{

MessageBox.Show(e.Message);
return null;
}

}

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

public List ReieveList(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(dt);

return list;
}
catch (Exception e)
{

MessageBox.Show(e.Message);
return null;
}

}

//插入一条数据
public bool Insert(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 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 s
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇十七、PythonSQLAlchemy 下一篇centos7安装navicat

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: