热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

今天看了事务的一个小小的总结

程序如下:让它记录下我对它的理解吧!classProgram{staticvoidMain(string[]args){stringconStrDat

   程序如下:让它记录下我对它的理解吧!

class Program
    {
        static void Main(string[] args)
        {
            string conStr=@"Data Source=MICROSOF-E258B5\SQLSERVER2008;Initial Catalog=PB_BookShopDemo;Integrated Security=True";
            string[] paraTran = new string[] { "d","fd","dfas","fafdf"};
            using (SqlConnection con = new SqlConnection(conStr))
            {
               
                using (SqlCommand cmd = con.CreateCommand())
                {
                    con.Open();
                    SqlTransaction tran = con.BeginTransaction();
                    cmd.Transaction = tran;
                    try
                    {
                        for (int i = 0; i                         {
                            string sql = "insert into T_Student(Name) values ('"+paraTran[i]+"')";//这里注意拼接sql语句!
                            cmd.CommandText = sql;
                            cmd.ExecuteNonQuery();
                        }
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        tran.Rollback();
                    }
                    finally
                    {
                        con.Close();
                        Console.WriteLine("插入成功!");
                        Console.ReadKey();
                    }

                }
            }
        }
    }

 

转:https://www.cnblogs.com/mmdlp/archive/2011/11/07/2240404.html



推荐阅读
author-avatar
-54你懂不懂
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有