ACCESS数据库访问组件(二)

access|访问|数据|数据库

ACCESS数据库访问组件(二)ACCESS_Table.cs

using System;

namespace XLang.VideoOnline.Framework.Database.Access
{
/// <summary>
/// Summary description for ACCESS_DataTable.
/// </summary>
public class DataTable:System.Data.DataTable
{
private string _tableName;

private string _primaryKey;

public string Name
{
get { return _tableName; }
set { _tableName=value; }
}

public string PrimaryKey
{
get { return _primaryKey; }
set { _primaryKey=value; }
}

public DataTable()
{
}

public DataTable(string tableName)
{
_tableName=tableName;
}

public string CreateCommand(Database.Access.DataRow[] rows)
{
string temp="CREATE TABLE "+_tableName+"(";
for(int i=0;i<rows.GetLength(0);i++)
{
temp+="["+rows[i].Name+"] "+rows[i].Type+(rows[i].IsNull? ",":" not null,");
}
temp+="CONSTRAINT [Index1] PRIMARY KEY (["+rows[0].Name+"]))";
_primaryKey=rows[0].Name;
return temp;
}

public string InsertCommand(Database.Access.DataRow[] rows)
{
string temp="";
temp="INSERT INTO "+_tableName+" ( ";
for(int i=0;i<rows.GetLength(0);i++)
{
temp+="["+rows[i].Name+"] "+",";
}
temp=temp.Substring(0,temp.Length-1);
temp+=") VALUES(";
for(int i=0;i<rows.GetLength(0);i++)
{
if(rows[i].Type.Equals("boolean"))
temp+=((rows[i].Value.ToString().ToUpper().Equals("YES"))? "yes":"no")+",";
else if(rows[i].Type.StartsWith("string")||rows[i].Type.Equals("memo")||
rows[i].Type.Equals("text")||rows[i].Type.StartsWith("text"))
temp+="'"+rows[i].Value+"',";
else if(rows[i].Type.Equals("date"))
temp+="#"+rows[i].Value+"#,";
else
temp+=rows[i].Value+",";
}
temp=temp.Substring(0,temp.Length-1);
temp+=")";
return temp;
}

public string DeleteCommand()
{
return "DROP TABLE "+_tableName;
}

public string DeleteCommand(string conditions)
{
return "DELETE * FROM "+_tableName+" WHERE "+conditions;
}

public string UpdateCommand(Database.Access.DataRow[] rows,string conditions)
{
string temp="UPDATE "+_tableName+" SET ";
temp+=ProsessDifferentDataType(rows);
temp+=" WHERE "+conditions;
return temp;
}

public string SelectCommand()
{
return "SELECT * FROM "+_tableName;
}

public string SelectCommand(string conditions)
{
return "SELECT * FROM "+_tableName +" WHERE "+conditions;
}

public string SelectCommand(Database.Access.DataRow[] rows,string conditions)
{
string temp="SELECT ";
for(int i=0;i<rows.GetLength(0);i++)
{
temp+="["+rows[i].Name+"],";
}
temp=temp.Substring(0,temp.Length-1);
temp+=" FROM "+_tableName+" WHERE "+conditions;
return temp;
}

public string JoinCommand(Database.Access.DataRow[] rows,string condition,params string [] tableNames)
{
string temp="SELECT ";
for(int i=0;i<rows.GetLength(0);i++)
{
temp+="["+rows[i].Name+"],";
}
temp=temp.Substring(0,temp.Length-1);
// temp+=" FROM "+_tableName+" WHERE "+conditions;
temp+=" FROM ";
foreach(string table in tableNames)
temp+=table+",";
temp=temp.Substring(0,temp.Length-1);

if(condition!=null)
temp+=" WHERE "+condition;

return temp;
}

private string ProsessDifferentDataType(Database.Access.DataRow[] rows)
{
string temp="";
for(int i=0;i<rows.GetLength(0);i++)
{
if(rows[i].Type.Equals("boolean"))
temp+="["+rows[i].Name+"] ="+
((rows[i].Value.ToString().ToUpper().Equals("YES"))? "yes":"no")
+",";
else if(rows[i].Type.StartsWith("string")||rows[i].Type.Equals("memo")||
rows[i].Type.Equals("text")||rows[i].Type.StartsWith("text"))
temp+="["+rows[i].Name+"] ='"+rows[i].Value+"',";
else if(rows[i].Type.Equals("date"))
temp+="["+rows[i].Name+"] =#"+rows[i].Value+"#,";
else
temp+="["+rows[i].Name+"] ="+rows[i].Value+",";

}
temp=temp.Substring(0,temp.Length-1);
return temp;
}
}
}

时间: 2024-07-30 10:02:35

ACCESS数据库访问组件(二)的相关文章

ACCESS数据库访问组件(一)

access|访问|数据|数据库 ACCESS数据库访问组件(一)ACCESS_Database.cs using System;using System.Data;using System.Data.OleDb;using System.Collections; namespace XLang.VideoOnline.Framework.Database.Access{ /// <summary> /// XLang.VideoOnline.Framework.Database is des

ACCESS数据库访问组件(三)

access|访问|数据|数据库 using System;using System.Data;using System.Data.OleDb;using System.Collections; namespace XLang.VideoOnline.Framework.Database.Access{ /// <summary> /// Summary description for ACCESS_DataTablesCollection. /// </summary> publ

ACCESS数据库访问组件(四)

access|访问|数据|数据库 using System;using System.Data;using System.Data.OleDb;using System.Collections; namespace XLang.VideoOnline.Framework.Database.Access{ /// <summary> /// Summary description for ACCESS_DataViewsCollection. /// </summary> publi

[求助]Access数据库访问与openfiledialog

问题描述 环境:VS2010,ACCESS2010,WIN7问题:用C#开放一个软件,两个功能,一个是ACCESS数据库访问,一个是用openFileDialog对话框选取文件.在软件运行时,如果先执行openFileDialog,则没有问题,数据库访问正常,以后再执行openFileDialog也没问题,但是,如果先执行数据库访问,再执行openFileDialog时,在OpenFileDialog1.ShowDialog(this)这句就会出错,或者文件浏览对话框无响应.报错信息是:尝试读取

ACCESS数据库访问的类

大部分ASP应用,都离不开对数据库的访问及操作,所以,对于数据库部分的访问操作,我们应该单独抽象出来,封装成一个单独的类.如果所用语言支持继承,可以封装一个这样的类,然后在数据操作层继承即可.下面是我写的一个ACCESS数据库访问的类,针对ACCESS作了优化,不过因为缺少足够的应用测试,可能仍然存在未知的bug及应用限制,主要代码如下: <% Class Oledb Private IDataPath Private IConnectionString Private Conn Private

SQL Artisan数据库访问组件功能概述

本文概述SQL Artisan数据库访问组件功能. SQL Artisan现有的版已经在项目中运用,在使用的过程中得到的效果相当理想.刚接触这个组件的几个新同事通过了解已有例子,很快就能适应到项目开发过程中.组件的对象操作和编译检测大提高了编写效率,在项目中得到的效果自己也有点意想不到. SQL Artisang下一个版本的功能主完善在表对象操作和对象映射方面;包括:表对象支持数据操作,对象继承,视图对象映射,统计对象映射等.为了让组件功能扩展更方便,把组件的数据映射方式进行重构,由原来的XML

Access数据库访问助手类

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using System.Collections; namespace TaoBaoSyncLibrary.Dal { /// <summary> /// Access数据库访问助手类 /// </summary> public class DBHelper_Ac

论数据库访问组件的选择--火地晋大作读后感

前言 火地晋做了一件有意义的事情.把这些ORM对比了一下(http://www.cnblogs.com/yelaiju/p/3209506.html). 这里要讨论一下我们用一个什么样的策略来选择数据库访问组件.通常有如下几种情况来选择: 1. 基于过去的经验    比如过去用过某某ORM,在将来的项目中继续用的话经验和熟练度就会比较高.这是建立在对该ORM的信任基础之上的. 2. 别人介绍或者在网上自己发现的,然后再试用也不错   这种情况也挺普遍的.业界同事介绍某某ORM不错,或者在网络上发

Wince MFC OLE DB SQLCE数据库访问技术(二):嵌入式目标平台创建本地数据库sdf文件

前言 上一节已经讲述了嵌入式目标平台上安装sqlCE,本章将介绍如何在目标平台上创建本地数据库sdf文件. 备注:博客中所有关于Wince MFC OLE DB   SQLCE数据库访问技术的文章都是基于SQL Server 2005 Compact Edition即 sqlCE 3.x     在讲述sqlCE之前,先来了解下,sqlCE优于wince 自带数据库的特点: 类别 对象 最大大小限制 存储 列名 128 个字符   表中的列数 1024 行大小 8060 字节   数据库密码 4