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. bq24075 锂电池 充电电路分析

     bq24075 锂电池 充电电路分析 本文主要是分析bq24075锂电池充电芯片电路,知道其大致是怎么工作的,其中的一些电阻该如何配置. -- 深圳 南山平山村 曾剑锋 一.参考文章: . NTC热 ...

  2. HDU 5301 Buildings 建公寓(逻辑,水)

    题意:有一个包含n*m个格子的矩阵,其中有一个格子已经被染黑,现在要拿一些矩形来填充矩阵,不能填充到黑格子,但是每一个填充进去的矩形都必须至少有一条边紧贴在矩阵的边缘(4条边)的.用于填充的矩形其中最 ...

  3. shell 的判断与比较

    1  shell 的$! ,$?, $$,$@ $n        $1 the first parameter,$2 the second... $#        The number of co ...

  4. MySQL基础之第2章 Windows平台下安装与配置MySQL

    2.1.msi安装包 2.1.1.安装 特别要注意的是,安装前要删除原来的my.ini和原来的data目录,改名也行,不然在最后一步会“apply security settings”报个1045错误 ...

  5. MyBatis 入门到精通(三) 高级结果映射

    MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程序,也将是非常 ...

  6. Dapper.net 在Parameterized时对于String的扩展(转)

    虽然Dapper通过提供的DbString本身支持对于String的指定Parameterized,但这方法明显不够,当Insert时,我们更希望是把一个Poco直接传递过去,而不是来new一个匿名函 ...

  7. XCode修改工程名注意

    以下文字转载过来,在使用的过程中遇到几个问题 1.需要在 Build phases 里面,检查下 Link Binary With Libraries 以及Compline Sources 2.Bul ...

  8. div模拟的下拉框特效jquery

    从网上找来的,感觉不错就拿来分享下 <style type="text/css"> body, ul, li { margin: 0; padding: 0; font ...

  9. NGUI的UIProgressBar使用裁剪方式而不是压缩方式的方法

    UIProgressBar默认的方式是压缩图片,而如果我们需要裁减图片,只需要将UIProgressBar的Foreground的UISprite的Type改为Filled就行了. 好几个“的”... ...

  10. MFC记住上次路径---遇到的一些问题

    今天完成一个需求,就是记住用户选择的文件路径,先是熟悉代码,然后在网上找解决方法,一开始感觉没什么,网上的方法差不多,应该很容易做出来,结果真是卡了一半天,到晚上自己才慢慢的搞清楚了. 遇到的问题真不 ...