惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
D
Docker
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
IT之家
IT之家
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
B
Blog
D
DataBreaches.Net

博客园 - xumingming

C# 长浏览 SQL Server 的位运算 sql 的随机函数newID()和RAND() Linq To Xml (增,删,改,查) - xumingming sp_executesql的用法 Sql Server 存储过程分页 linq 存储过程返回多个结果集 Sql语句访问WEB 一些常用的类 MVC Controller与ActionResult的返回值 - xumingming - 博客园 IEnumerable_T_、IEnumerable、ICollection_T_、IList_T_、ObservableCollectin_T_和Collection_T_得关系 routes.MapRoute - xumingming - 博客园 JQuery与Ajax常用代码 MVC 小笔记 - xumingming - 博客园 GridView 的临时增删改查示例 GridView 72般绝技 VS2005中将GridView 中的数据导出至excel - xumingming - 博客园 触发器 删除数据库表中的信息(适用于被注入)
C#代码中的事务 - xumingming - 博客园
xumingming · 2009-07-27 · via 博客园 - xumingming

private void button1_Click(object sender, EventArgs e)
        {

            // 显示事务

            SqlTransaction tran;

            string sConn = "server=.\\DS;database=T105_2005;uid=sa;pwd=sasa";
            string sql1 = "update zh set zm=zm-"+Convert.ToDouble(this.textBox1.Text.Trim())+" where zid='1002'";
            string sql2 = "update zh set zm=zm+" + Convert.ToDouble(this.textBox1.Text.Trim()) + " where zid='1001'";

            SqlConnection con = new SqlConnection(sConn);
            con.Open();
            tran = con.BeginTransaction();
            SqlCommand com = new SqlCommand();
            try
            {

                com.Connection = con;
                com.Transaction = tran;


             
                com.CommandText = sql1;
                com.ExecuteNonQuery();
                com.CommandText = sql2;
                com.ExecuteNonQuery();
                con.Close();

                MessageBox.Show("帐装成功");
                tran.Commit();
            }
            catch
            {
                MessageBox.Show("帐装失败");
                tran.Rollback();
            }


           
        }
    }