private void toolStripButton1_Click(object sender, EventArgs e)
{
//判断新增的年度是否已经存在
if (HasYear())
{
MessageBox.Show("该年度分配给各单位的任务已经存在!");
return;
}
else
{
#region 方法总结
//var q = from p in mf.DS.UnitPaperTask.AsEnumerable()
// where p.year == tscombYear.SelectedItem.ToString()
// select p;
//foreach (var i in q)
//{
//这个命令是直接插入库中,系统由于IO问题会非常慢,有假死现象
// mf.unitpapertaskTap.Insert(i.unitid, i.paperid, i.plantnum, tscombNewYear.SelectedItem.ToString().Trim()); //}
//mf.unitpapertaskTap.Update(mf.DS.UnitPaperTask);
//mf.unitpapertaskTap.Dispose();
//mf.unitpapertaskTap.Fill(mf.DS.UnitPaperTask);
//
//解决办法,在内存中把表筛选后,复制一个datatable,循环改year值的同时“在内存中”加入新行值。最后提交一次提交到库
//通过实践,updata也是一行一行的增加到数据库,同样的慢,看来上面的方法也对。 #endregion #region 解决方法二也是不行 //string oldYear = tscombYear.SelectedItem.ToString();
//string newYear = tscombNewYear.SelectedItem.ToString(); //DataTable dt = mf.DS.UnitPaperTask.Select("year='" + oldYear + "'").CopyToDataTable();
////dataGridView1.DataSource = dt;
//foreach (DataRow dr in dt.Rows)
//{
// dr["year"] = newYear;
// //内存中加入行 可用 dataGridView1.DataSource = mf.DS.UnitPaperTas;显示出来 // mf.DS.UnitPaperTask.AddUnitPaperTaskRow(dr["unitid"].ToString(), dr["paperid"].ToString(), Convert.ToInt32(dr["plantnum"].ToString()), dr["year"].ToString());
//} ////dataGridView1.DataSource = mf.DS.UnitPaperTask;
////也是到数据库了,同样的很慢。
//mf.unitpapertaskTap.Update(mf.DS.UnitPaperTask); //删除再填充是为了取得ID
//mf.unitpapertaskTap.Dispose();
//mf.unitpapertaskTap.Fill(mf.DS.UnitPaperTask); #endregion #region 解决方法三采用事务处理 string oldYear = tscombYear.SelectedItem.ToString();
string newYear = tscombNewYear.SelectedItem.ToString();
string datasource = ConfigurationManager.ConnectionStrings["DBzd.Properties.Settings.baokanConnectionString"].ConnectionString.ToString();
var qUnitTask = from p in mf.DS.UnitPaperTask.AsEnumerable() where p.year == oldYear select p;
var qComnyTask = from p in mf.DS.UnitCompMoney.AsEnumerable() where p.year == oldYear select p;
//加入了详细的任务列表
using (SQLiteConnection conn = new SQLiteConnection(datasource))
{
conn.Open();
using (System.Data.SQLite.SQLiteTransaction trans = conn.BeginTransaction())
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
cmd.Transaction = trans;
try
{
foreach (var i in qUnitTask)
{
cmd.CommandText = @"INSERT INTO UnitPaperTask(unitid,paperid,plantnum,year) VALUES('" + i.unitid+ "','" +i.paperid + "','" + i.plantnum + "','" + newYear + "')";
cmd.ExecuteNonQuery();
} trans.Commit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
trans.Rollback(); }
}
}
} //需要加入企业任务和指定金额的任务单位
using (SQLiteConnection con = new SQLiteConnection(datasource))
{
con.Open();
using (SQLiteTransaction tran = con.BeginTransaction())
{
using (SQLiteCommand cm = new SQLiteCommand(con))
{
cm.Transaction = tran;
try
{
foreach (var i in qComnyTask)
{
cm.CommandText = @"insert into UnitCompMoney(unitid,compprices,year) values ('" + i.unitid + "','" + i.compprices + "','" + newYear + "')";
cm.ExecuteNonQuery();
}
tran.Commit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
tran.Rollback();
}
}
}
} #endregion #region 第四种方法。用SQLiteHelper实现--最后是实再不了的,因为参数只能传递一次,不能随着循环而改变值
//string oldYear = tscombYear.SelectedItem.ToString();
//string newYear = tscombNewYear.SelectedItem.ToString();
//DataTable dt = mf.DS.UnitPaperTask.Select("year='" + oldYear + "'").CopyToDataTable(); //SQLiteParameter[] Paras = new SQLiteParameter[] {
// new SQLiteParameter("@unitid",dt.Rows[0][1].ToString()),
// new SQLiteParameter("@paperid",dt.Rows[0][2].ToString()),
// new SQLiteParameter("@plantnum",dt.Rows[0][3].ToString()),
// new SQLiteParameter("@year",newYear)
//};
//string sql = "insert into UnitPaperTask(unitid,paperid,plantnum,year) VALUES ( @unitid,@paperid,@plantnum,@year)"; //int rs = SQLiteHelper.TransExecuteNonQuery(dt, sql, Paras);
//MessageBox.Show("增加新年度任务成功:"+rs.ToString()); #endregion #region 第5种方法 因为第3种是本地事处理,虽然很好的实现功能,现在想把两个表的操作放在一个分布式事务中。--这样不行SQLite没有 CommittableTransaction #endregion mf.unitpapertaskTap.Dispose();
mf.unitpapertaskTap.Fill(mf.DS.UnitPaperTask);
mf.unittaskmoeyTap.Dispose();
mf.unitpapertaskTap.Fill(mf.DS.UnitPaperTask); AddToolYear();
}
}

SQLite本地事务处理的更多相关文章

  1. sqlite本地保存数据

    package com.cesecsh.ics.database; import android.content.Context; import android.database.Cursor; im ...

  2. Android 使用SQLite本地数据库

    参考:http://blog.csdn.net/jianghuiquan/article/details/8569252 在Android平台上,集成了一个嵌入式关系型数据库—SQLite.以SQLi ...

  3. [转载]Unity3D 游戏引擎之使用C#语言建立本地数据库(SQLITE)

    以前在开发中一直使用IOS源生的数据库,通过传递消息的形式在与Unity3D中进行交互.本文我在详细说说如何使用C#语言来在MAC 操作系统下创建Unity本地数据库,我是C#控哇咔咔--- 首先你需 ...

  4. SQLite入门与分析(四)---Page Cache之事务处理(1)

    写在前面:从本章开始,将对SQLite的每个模块进行讨论.讨论的顺序按照我阅读SQLite的顺序来进行,由于项目的需要,以及时间关系,不能给出一个完整的计划,但是我会先讨论我认为比较重要的内容.本节讨 ...

  5. Xamarin.Forms 使用本地数据库之 SQLite

    前言 Xamarin.Forms支持使用SQLite数据库引擎.本文介绍了Xamarin.Forms应用程序如何读取和写入数据到使用SQLite.Net的本地SQLite数据库. 在Xamarin.F ...

  6. SQLite做为本地缓存的应用需要注意的地方

    原文:SQLite做为本地缓存的应用需要注意的地方 今天看到了园友陆敏计的一篇文章<<C#数据本地存储方案之SQLite>>, 写到了SQLite的诸多优点,尤其适应于本地数据 ...

  7. Swift使用FMDB操作SQLite

    SQLite大家都懂的.本地数据库,在移动设备上使用广泛.IOS平台上自然也少不了它.最近自己折腾一个小App的时候需要使用sqlite本地数据库,上Github搜了下IOS下对SQLite的三方封装 ...

  8. Sqlite查询时间段内的数据问题解决!

    最近搞Sqlite本地查询,需求为查询某时间段内的数据,在SQL中我们都知道为: select * from tblName where rDate Between '2008-6-10' and   ...

  9. SQLite函数详解之二

    sqlite3支持的数据类型: NULL.INTEGER.REAL.TEXT.BLOB 但是,sqlite3也支持如下的数据类型 smallint           16位整数 integer    ...

随机推荐

  1. Jqgrid demo-史上最强大,没有之一

            为了大家能够更好的学习和使用Jqgrid网格插件,我决定用Strtus2+Spring+hibernate+Jquery+Jqgrid实现一个Jqgrid网格插件的demo.当然官方网 ...

  2. Android 系统内置App JNI

    说明 将Android应用作为系统内置遇到一些问题: 一个是使用Android源码的mmm命令生成的JNI名字和使用NDK生成的JNI的名字是不一样的: 另外就是AndroidManifest.xml ...

  3. Go2Shell 打开设置窗口

    Go2Shell默认没有设置界面,需要用命令行打开 在Shell中输入: open -a Go2Shell --args config

  4. 查看nginx编译安装

    大家是否遇到过去了新公司,公司内的LAMP,LNMP等所有的环境都是配置好的(已经在提供服务了),公司又没有留下部署文档,甚至安装LAMP,LAMP等环境的人已经和你交接完离职了,那么线上服务器(la ...

  5. 【转】cocos2d-x 3x Sprite3D

    Sprite3D Sprite3D works in many ways like a normal Sprite. Sprite3D is a three-dimensional model tha ...

  6. iOS-利用AFNetworking(AFN 1.x)-实现文件上传

    转:http://www.kaifazhe.com/ios_school/380067.html 官方建议AFN的使用方法 1. 定义一个全局的AFHttpClient:包含有 1> baseU ...

  7. codeforces 680E Bear and Square Grid 巧妙暴力

    这个题是个想法题 先预处理连通块,然后需要用到一种巧妙暴力,即0变1,1变0,一列列添加删除 复杂度O(n^3) #include <cstdio> #include <iostre ...

  8. IOS init initWith 等相关集中

    1.initWithCoder    当一个view从nib初始化的时候,会调用这个函数.  用keyedArchiver序列化一个类的实力,后面用keyedUnArchiver拿回来的时候会调用到 ...

  9. 如何解决Rally模板提示angular js加载错误

    [前言] Rally是一个开源测试工具,用于测试openstack各个组件的性能 在使用Rally测试完毕后,一般会生成测试报告,这点很重要.但是原生态的Rally报告模板angular js框架是从 ...

  10. "_ITERATOR_DEBUG_LEVEL"的不匹配项: 值"0"不匹配值"2"

    error: 1>vtkCommon.lib(vtkDebugLeaksManager.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项 ...