1

public static class DataTableHelper
{
public static List<T> ToModel<T>(this DataTable dt) where T : class ,new()
{
if (dt == null || dt.Rows.Count == )
return null;
Type type = typeof(T);
var list = new List<T>();
for (int row = ; row < dt.Rows.Count; row++)
{
Object obj = type.Assembly.CreateInstance(type.FullName); System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties(); for (int i = ; i < Props.Length; i++)
{
if (Props[i].CanWrite && dt.Columns.IndexOf(Props[i].Name) > -)
{
Props[i].SetValue(obj, dt.Rows[row][Props[i].Name], null);
}
} list.Add(obj as T);
} return list;
}
}
public class StudentModel
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreateTime { get; set; }
}
//数据库访问对象
public class Studuent
{
public static IList<StudentModel> GetList()
{
//不允许脏读
string sql = "SELECT Id,Name,CreateTime FROM Student ORDER BY Id DESC;";
DataTable dt = SqlHelper.Instance.ExecuteDataTable(CommandType.Text, sql);
return dt.ToModel<StudentModel>();
} public static bool Add(string name)
{
SqlParameter[] parms = {
new SqlParameter("@Name", SqlDbType.NVarChar, ) { Value = name },
new SqlParameter("@CreateTime", SqlDbType.DateTime) { Value = DateTime.Now }
};
string sql = "INSERT INTO Student (Name,CreateTime) VALUES (@Name,@CreateTime)";
return SqlHelper.Instance.ExecuteNonQuery(CommandType.Text, sql, parms) > ;
} public static void Clear()
{
SqlHelper.Instance.ExecuteNonQuery("DELETE From Student");
} //公共方法,输出学生列表 }
public class TransactionScopeTest
{
static void PrintStudent()
{
IList<StudentModel> list = Studuent.GetList();
if (list == null)
return;
foreach (var item in list)
{
Console.WriteLine("{0}\t{1}\t{2}", Thread.CurrentThread.ManagedThreadId, item.Name, item.CreateTime);
}
Console.WriteLine();
} public static void Excute()
{
Studuent.Clear();
Task.Run(() =>
{
Console.WriteLine("开始添加用户");
using (TransactionScope scope = new TransactionScope())
{
Studuent.Add("Grace");
Studuent.Add("Aven");
scope.Complete();
//在Compltete已提交数据库
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "Scope已提交");
Thread.Sleep( * );
} //在作用范围外解除锁表
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "scope已释放");
});
//PrintStudent();
}
}

TransactionScope小例的更多相关文章

  1. linux 命令小例

    xargs示例: ls |xargs -i mv {}  /opt find示例: find -mtime +n -name “*.avi” -type f -exec rm {} \; find - ...

  2. Ubuntu13.04 Eclipse下编译安装Hadoop插件及使用小例

    Ubuntu13.04 Eclipse下编译安装Hadoop插件及使用小例 一.在Eclipse下编译安装Hadoop插件 Hadoop的Eclipse插件现在已经没有二进制版直接提供,只能自己编译. ...

  3. 使用libcurl下载文件小例

    libcurl是一个很强大的开源网络处理库,支持包括HTTP.HTTPS.FTP……一系列网络协议.用它来进行HTTP的get\post 或者下载文件更是小菜一碟,chrome内核都用到了它,本文主要 ...

  4. webpack -- 多页面简单小例

    有时单页面并不能满足我们的业务需求,就需要去构建多页面应用,以下为简单小例: entry:{ index:'./src/module/index/index.js', student:'./src/m ...

  5. [libpng]CMake+VS2015下编译libpng,及使用小例

    编译前的工作 在编译libpng前,需要把zlib编译好,并加载到编译环境里. CMake + VS2015 下编译zlib,及使用小例 下载与解压 libpng的官网是 http://www.lib ...

  6. HTML5基础小结(二)——标签小例

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1amlhaGFuNjI5NjI5/font/5a6L5L2T/fontsize/400/fill/I0 ...

  7. js入门学习~ 运动应用小例

    要实现的效果如下: 鼠标移入各个小方块,实现对应的效果(变宽,变高,移入透明,移出恢复)~~ (且各运动相互之前不干扰)  主要是练习多个物体的运动框架~~ --------------------- ...

  8. js中的冒泡排序以及实现一个数组中得最到最大的数字小例

    这其实是一个很简单的js就可以实现,当然一般情况就是利用for循环,从第一个跟第二个开始比较,根据大小交互位置,思路很简单. 也就是js中的冒泡排序 冒泡排序 时间复杂度为O(n^2),有两个优点: ...

  9. linux之C编程实战小例

    人生匆匆一趟,打不打酱油?怎么打?怎么打"质量好点的酱油"?由你决定.打酱油是一种态度,更是一种生活! 哈哈,事不关己不开口,专心一意打酱油! 请记住下面些许话: 不要一味的说别人 ...

随机推荐

  1. Python程序调试-TabError: inconsistent use of tabs and spaces in indentation

    报错信息:TabError: inconsistent use of tabs and spaces in indentation 说明:代码缩进统一使用Tab键或空格键,不能混用. 解决办法: 1. ...

  2. PCL点云库中的坐标系(CoordinateSystem)

    博客转载自:https://blog.csdn.net/qq_33624918/article/details/80488590 引言 世上本没有坐标系,用的人多了,便定义了坐标系统用来定位.地理坐标 ...

  3. 编写高质量代码改善C#程序的157个建议——建议23:避免将List<T>作为自定义集合类的基类

    建议23:避免将List<T>作为自定义集合类的基类 如果要实现一个自定义的集合类,不应该以一个FCL集合类为基类,反而应扩展相应的泛型接口.FCL结合类应该以组合的形式包含至自定义的集合 ...

  4. C# 取得内网IP、外网IP、客户端IP方法

    前言 在 Windows Form Application 里对于取得 IP Address 有内网.外网两种 IP Address ,如果只需要取得内网 IP Address ,可以透过使用 IPH ...

  5. wp7启动+幻灯片效果

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Wi ...

  6. ubuntu 14.04 x64下安装libreoffice

    LibreOffice是ubuntu 上的办公软件很多人都知道微软公司的的Word.Excel.PowerPoint和Outlook,但是很少有人知道LibreOffice. LibreOffice靠 ...

  7. 安装Xamarin.Android几个经典介面

    昨晚Microsoft MVP的身份来申请Xamarin.Android,想不到今早就有邮件回复.花上些少时间订阅与注册: 望有时间能学习到一些新技术. 下面是Insus.NET下载并安装,留下几个经 ...

  8. Spring boot进阶-配置Controller、interceptor...

    1.配置SpringBootApplication(对spring boot来说这是最基本) package io.github.syske.springboot31; import org.spri ...

  9. PTA数据结构之 List Leaves

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  10. 51nod1464(trie + dfs)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1464 题意: 中文题诶~ 思路: 将所有半回文串构建成一棵字 ...