一个人没事,写了一个底层数据实现读取设计模式,个人觉得还是蛮好扩展,里面有不足的地方希望大家给予指导。话不多说先看个图吧!图可能不正规,伤害了你的眼睛见谅。有图有真相

其实这个设计模式,就是一个简单的创建模式。什么是创建模式,我就简单的说一下,利用创建者模式可以创建出不同类型的产品。

首先我定义了一个SqlDB类这个类是实现数据连接以及Sql语句处理的类。SqlHelper类是存放sql语句的。Model是存放数据表字段封装的模型。IExcuteData是个泛型接口,通过这个接口可以得到不同模型类的一条数据。ExecuteData<T> 是个泛型类。不好意图忘了画。通过这个泛型类获得模型所有数据以及执行sql的语句其他语句。接下来我们就看看代码吧。

public static class SqlHelper
{

public const string Coumn_FOREIGNTRAIN_TrainID = "TrainID";
public const string Coumn_FOREIGNTRAIN_EmployeeNo = "EmployeeNo";
public const string Coumn_FOREIGNTRAIN_EmployeeName = "EmployeeName";
public const string Coumn_FOREIGNTRAIN_CourseNo = "CourseNo";
public const string Coumn_FOREIGNTRAIN_CredentialsNo = "CredentialsNo";
public const string Coumn_FOREIGNTRAIN_Credentials = "Credentials";
public const string Coumn_FOREIGNTRAIN_IDNumber = "IDNumber";
public const string Coumn_FOREIGNTRAIN_GetCourse = "GetCourse";
public const string Coumn_FOREIGNTRAIN_Unit = "Unit";
public const string Coumn_FOREIGNTRAIN_Qualified = "Qualified";
public const string Coumn_FOREIGNTRAIN_Isdelete = "Isdelete";
public const string Coumn_FOREIGNTRAIN_CreatDate = "CreatDate";
public const string Coumn_FOREIGNTRAIN_TrainNo = "TrainNo";
public const string Coumn_FOREIGNTRAIN_Explain = "Explain";

public const string Sql_FOREIGNTRAIN_GetAll = "select * from T_TMS_FOREIGNTRAIN where Isdelete=1 order by CreatDate desc";
public const string Sql_FOREIGNTRAIN_Add = "insert [dbo].[T_TMS_FOREIGNTRAIN](TrainID,EmployeeNo,EmployeeName,CourseNo,CredentialsNo" +
",Credentials,IDNumber,GetCourse,Unit,Isdelete,Qualified,CreatDate,TrainNo,Explain)" +
"values('{0}',{1},'{2}','{3}','{4}','{5}','{6}','{7}','{8}',{9},'{10}','{11}','{12}','{13}')";
public const string Sql_FOREIGNTRAIN_DeleteById = "delete from T_TMS_FOREIGNTRAIN where TrainID='{0}'";
#region

}

public interface IExcuteData<T>
{
  T GetInfo(DataRow dr);
}

model 模型要继承IExcuteData

public class T_TMS_FOREIGNTRAIN : DBModule.IExcuteData<T_TMS_FOREIGNTRAIN>
{
public T_TMS_FOREIGNTRAIN()
{ }
private string _trainid;
private int? _employeeno;
private string _employeename;
private string _courseno;
private string _credentialsno;
private string _credentials;
private string _idnumber;
private string _getcourse;
private string _unit;
private int? _isdelete;
private string _qualified;
private DateTime? _creatDate;
private string _trainNo;
private string _Explain;

public string Explain
{
get { return _Explain; }
set { _Explain = value; }
}

public string TrainNo
{
get { return _trainNo; }
set { _trainNo = value; }
}

public DateTime? CreatDate
{
get { return _creatDate; }
set { _creatDate = value; }
}
/// <summary>
///
/// </summary>
public string TrainID
{
set { _trainid = value; }
get { return _trainid; }
}
/// <summary>
///
/// </summary>
public int? EmployeeNo
{
set { _employeeno = value; }
get { return _employeeno; }
}
/// <summary>
///
/// </summary>
public string EmployeeName
{
set { _employeename = value; }
get { return _employeename; }
}
/// <summary>
///
/// </summary>
public string CourseNo
{
set { _courseno = value; }
get { return _courseno; }
}
/// <summary>
///
/// </summary>
public string CredentialsNo
{
set { _credentialsno = value; }
get { return _credentialsno; }
}
/// <summary>
///
/// </summary>
public string Credentials
{
set { _credentials = value; }
get { return _credentials; }
}
/// <summary>
///
/// </summary>
public string IDNumber
{
set { _idnumber = value; }
get { return _idnumber; }
}
/// <summary>
///
/// </summary>
public string GetCourse
{
set { _getcourse = value; }
get { return _getcourse; }
}
/// <summary>
///
/// </summary>
public string Unit
{
set { _unit = value; }
get { return _unit; }
}
/// <summary>
///
/// </summary>
public string Qualified
{
get { return _qualified; }
set { _qualified = value; }
}
/// <summary>
///
/// </summary>
public int? Isdelete
{
set { _isdelete = value; }
get { return _isdelete; }
}

#region IExcuteData<T_TMS_FOREIGNTRAIN> 成员

public T_TMS_FOREIGNTRAIN GetInfo(System.Data.DataRow dr)
{
T_TMS_FOREIGNTRAIN info = null;
if (dr != null)
{
info = new T_TMS_FOREIGNTRAIN();
info.CourseNo =PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_CourseNo]);
info.Credentials=PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_Credentials]);
info.CredentialsNo=PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_CredentialsNo]);
info.EmployeeName=PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_EmployeeName]);
info.EmployeeNo = PrettiChose.Convert.Getint(dr[SqlHelper.Coumn_FOREIGNTRAIN_EmployeeNo]);
info.GetCourse = PrettiChose.Convert.GetDateTimebystring(dr[SqlHelper.Coumn_FOREIGNTRAIN_GetCourse].ToString()).ToString("yyyy/MM/dd");
info.IDNumber = PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_IDNumber]);
info.Isdelete = PrettiChose.Convert.Getint(dr[SqlHelper.Coumn_FOREIGNTRAIN_Isdelete]);
info.TrainID = PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_TrainID]);
info.Unit = PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_Unit]);
info.Qualified = PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_Qualified]);
info.CreatDate = PrettiChose.Convert.GetDateTimebystring(dr[SqlHelper.Coumn_FOREIGNTRAIN_CreatDate].ToString());
info.TrainNo = PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_TrainNo]);
info.Explain = PrettiChose.Convert.Getstring(dr[SqlHelper.Coumn_FOREIGNTRAIN_Explain]);
}

return info;
}

#endregion
}

SqlDB是数据连接处理sql语句。

public class SqlDB
{

string sqlconn = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();
SqlConnection conn = null;
SqlTransaction trans = null;
public bool DbOpen()
{
bool btrn = false;
try
{
conn = new SqlConnection(sqlconn);
conn.Open();
btrn = true;
}
catch (Exception ex)
{
}
return btrn;
}
/// <summary>
/// 开始事物
/// </summary>
/// <returns></returns>
public bool DbOpenTransaction()
{
bool btrn = false;
try
{
conn = new SqlConnection(sqlconn);
conn.Open();
trans = conn.BeginTransaction();
btrn = true;
}
catch (Exception ex)
{

}

return btrn;
}
/// <summary>
/// 提交事物
/// </summary>
public void CommitTransaction()
{
trans.Commit();
}
/// <summary>
/// 释放事物
/// </summary>
public void DisposeTransaction()
{
trans.Dispose();
}

/// <summary>
/// 回滚事物
/// </summary>
public void RollbackTransaction()
{
trans.Rollback();
}

public void DbClose()
{
conn.Close();
}
/// <summary>
/// 执行事务
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public bool DoTransaction(string sql)
{
bool btrn = false;
try
{
SqlCommand cmd = new SqlCommand(sql, conn, trans);
cmd.ExecuteNonQuery();
btrn = true;

}
catch (Exception ex)
{
btrn = false;

}
return btrn;
}
/// <summary>
/// 得到数据
/// </summary>
/// <param name="sql"></param>
/// <param name="sResult"></param>
/// <returns></returns>
public DataSet GetDataSet(string sql, out string sResult)
{
sResult = "";
DataSet ds = null;
try
{
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
sResult = ex.Message;
}
return ds;
}
/// <summary>
/// 执行插入,删除,一条导入数据
/// </summary>
/// <param name="sql"></param>
/// <param name="sResult"></param>
/// <returns></returns>
public int ExecuteBySql(string sql, out string sResult)
{
sResult = "";
int icount = 0;
try
{
SqlCommand cmd = new SqlCommand(sql, conn);
icount = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
sResult = ex.Message;
}

return icount;
}
/// <summary>
/// 批量插入数据用的
/// </summary>
/// <param name="tablename"></param>
/// <param name="dataTable"></param>
/// <returns></returns>
public bool InsertTable(string tablename, DataTable dataTable)
{
bool trb = true;
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
{
bulkCopy.DestinationTableName = "dbo." + tablename;

try
{
bulkCopy.WriteToServer(dataTable);

}
catch (Exception ex)
{
trb = false;
}
finally
{
bulkCopy.Close();
}
}
return trb;
}
}

ExecuteData这个类是很重要,表现成得到数据,只要调用这里GetData方法就可以了。等sql 增删改,和事物

public class ExecuteData<T> where T :new()
{

SqlDB DB = new SqlDB();
/// <summary>
/// 得到传入的类型数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public virtual List<T> GetData(string sql)
{
List<T> CostList = null;
if (DB.DbOpen())
{
CostList = new List<T>();
string sResqul = "";
DataSet ds = DB.GetDataSet(sql, out sResqul);
//object k = new T();
//IExcuteData<T> TData = k as IExcuteData<T>;
dynamic TConvert = new T();
IExcuteData<T> TData = TConvert;
if (ds.Tables != null)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
T info = TData.GetInfo(ds.Tables[0].Rows[i]);
if (info != null)
{
CostList.Add(info);
}
}
}
DB.DbClose();
}
return CostList;
}

/// <summary>
/// 执行insert,update ,delete语句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public bool ExcuteAdd_Up_del(string sql)
{
bool btr = false;
if (DB.DbOpen())
{
string sResqul = "";
if (DB.ExecuteBySql(sql, out sResqul) > 0)
{
btr = true;
}
DB.DbClose();
}
return btr;
}

/// <summary>
/// 执行事务数据
/// </summary>
/// <param name="sqllist"></param>
/// <returns></returns>
public bool DoTransaction(List<string> sqllist)
{
bool btr = false;
try
{
if (DB.DbOpenTransaction())
{
for (int i = 0; i < sqllist.Count; i++)
{
if (DB.DoTransaction(sqllist[i]))
{
btr = true;
}
else
{
DB.RollbackTransaction();
break;
}
}
DB.CommitTransaction();
DB.DisposeTransaction();
DB.DbClose();
}
}
catch
{

}
return btr;
}
}

接下来就是在表现层做一下事情就可以得到数据,以及处理数据

ExecuteData<要传入的类型> excute=new ExecuteData<要传入的类型>();

excute.GetData("要传入sql")

excute.DoTransaction(“要执行的Sql数据集合”)

excute.ExcuteAdd_Up_del("增删改sql语句")

我想给大家看的不是代码,而是这个设计的想法。设计原理,代码可以根据情况而改。

C# 数据实现设计模式的更多相关文章

  1. 大数据算法设计模式(1) - topN spark实现

    topN算法,spark实现 package com.kangaroo.studio.algorithms.topn; import org.apache.spark.api.java.JavaPai ...

  2. 大数据算法设计模式(2) - 左外链接(leftOuterJoin) spark实现

    左外链接(leftOuterJoin) spark实现 package com.kangaroo.studio.algorithms.join; import org.apache.spark.api ...

  3. Spark Streaming源码解读之流数据不断接收全生命周期彻底研究和思考

    本期内容 : 数据接收架构设计模式 数据接收源码彻底研究 一.Spark Streaming数据接收设计模式   Spark Streaming接收数据也相似MVC架构: 1. Mode相当于Rece ...

  4. Spark Streaming源码解读之流数据不断接收和全生命周期彻底研究和思考

    本节的主要内容: 一.数据接受架构和设计模式 二.接受数据的源码解读 Spark Streaming不断持续的接收数据,具有Receiver的Spark 应用程序的考虑. Receiver和Drive ...

  5. MagicalRecord,一个简化CoreData操作的工具库

    简介 项目主页:https://github.com/magicalpanda/MagicalRecord 实例下载:https://github.com/ios122/MagicalRecord 在 ...

  6. Unity之串口通信(基于三姿态传感器)

    原地址:http://www.cnblogs.com/alongu3d/archive/2013/05/02/3054962.html /******************************* ...

  7. 我(webabcd)的文章索引

    [最后更新:2014.08.28] 重新想象 Windows Store Apps 系列文章 重新想象 Windows 8 Store Apps 系列文章 重新想象 Windows 8 Store A ...

  8. .NET牛人养成计划

    六大喜讯:(1)对于小型平板等授权免费(2)编译平台Rosly开源,ASP.NET全系平台开源(ASP.NET,Web API):ASP.NET跨平台,Mono,让ASP.NET运行在Linux和Un ...

  9. 凭这份pdf让我轻松拿下了蚂蚁金服、字节跳动、小米等大厂的offer

    关于程序员,除了做项目来提高自身的技术之外,还有一种提升自己的专业技能就是:多!看!书! 小编整理出一篇Java进阶架构师之路的核心知识,同时也是面试时面试官必问的知识点,篇章也是包括了很多知识点,其 ...

随机推荐

  1. 如何在Ubuntu 14.04中使用Samba共享文件

    Linux 下的文件共享利器 Samba 想必大家已经非常熟悉了,今天我们介绍下如何在Ubuntu 14.04中使用 Samba 共享网络文件.打印机和其它公共资源. 1.安装 Samba 和图形配置 ...

  2. Membership添加验证码登录

    1.在公共类ImageHelper中编写公共方法,产生随机验证码 /// <summary> /// 产生随机验证码 /// </summary> /// <return ...

  3. oracle 主键应用序列和触发器实现自动增长

    oracle 主键自动增长 这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下: create table simon_example ( id number(4) not null pr ...

  4. soapUI通过groovy脚本设置超时时间

    import com.eviware.soapui.SoapUI import com.eviware.soapui.settings.HttpSettings import com.eviware. ...

  5. iOS开发中文件的上传和下载功能的基本实现-备用

    感谢大神分享 这篇文章主要介绍了iOS开发中文件的上传和下载功能的基本实现,并且下载方面讲到了大文件的多线程断点下载,需要的朋友可以参考下 文件的上传 说明:文件上传使用的时POST请求,通常把要上传 ...

  6. data语意学

    引例: class X{}; class Y:public virtual X{}; class Z:public virtual X{}; class A:public Y,public Z{}; ...

  7. android textview 行间距

    设置行间距:android:lineSpacingExtra 设置行间距的倍数:android:lineSpacingMultiplier 如下:设置行间距为3,行间距倍数为1.5 <TextV ...

  8. cf C. Sereja and Algorithm

    http://codeforces.com/contest/368/problem/C 从左向右记录从1位置到每一个位置上x,y,z的个数.然后判断在l,r区间内的x,y,z的关系满不满足abs(x- ...

  9. cf B. Fence

    http://codeforces.com/contest/363/problem/B #include <cstdio> #include <cstring> #includ ...

  10. Android WebView简介

    Android的网络功能特别强大,WebView(网络视图)组件支持加载网页,可以理解为使用Webkit内核的浏览器,而它的实现方式有两种: 第一种具体实现步骤如下: (1)在布局文件中先生命WebV ...