当前位置: 首页 > news >正文

C#中 一次执行多条带GO的sql语句

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Text.RegularExpressions; using System.Data.SqlClient; namespace ConsoleApplication1 { class Program { //注: 在cmd.ExecuteNonQuery()是不允许语句中有GO出现的, 有则出错。 static string connectionString = "server=20111011-2204\\SQLSERVER2008;uid=ecuser;pwd=1234;database=Stu;"; static void Main(string[] args) { string sql = @"Alter table Student add DateBak varchar(16) go Update Student set DateBak = convert(char,getdate(),101) go Update Student set Memo = DateBak go Alter table Student drop column DateBak go "; Console.WriteLine("1. 不用事务:"); ExecuteSqlWithGo(sql); Console.WriteLine("2. 用事务:"); ExecuteSqlWithGoUseTran(sql); Console.ReadLine(); } public static void ExecuteSqlWithGo(String sql) { int effectedRows = 0; using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; try { //注: 此处以 换行_后面带0到多个空格_再后面是go 来分割字符串 String[] sqlArr = Regex.Split(sql.Trim(), "\r\n\\s*go", RegexOptions.IgnoreCase); foreach (string strsql in sqlArr) { if (strsql.Trim().Length > 1 && strsql.Trim()!="\r\n") { cmd.CommandText = strsql; effectedRows = cmd.ExecuteNonQuery(); } } } catch (System.Data.SqlClient.SqlException E) { throw new Exception(E.Message); } finally { conn.Close(); } } } public static void ExecuteSqlWithGoUseTran(String sql) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { //注: 此处以 换行_后面带0到多个空格_再后面是go 来分割字符串 String[] sqlArr = Regex.Split(sql.Trim(), "\r\n\\s*go", RegexOptions.IgnoreCase); foreach (string strsql in sqlArr) { if (strsql.Trim().Length > 1 && strsql.Trim() != "\r\n") { cmd.CommandText = strsql; cmd.ExecuteNonQuery(); } } tx.Commit(); } catch (System.Data.SqlClient.SqlException E) { tx.Rollback(); throw new Exception(E.Message); } finally { conn.Close(); } } } } }

相关文章:

  • 贪吃蛇-需求分析
  • C#正则表达式(RegEx)高级应用之分组(Group)替换(Replace)
  • algorithm.sty not found error in LaTeX 解决方法
  • C# regex replace
  • linux 的 awk 使用
  • Linux启动检测磁盘失败
  • 简历已经过时了,而这里正是你需要的
  • 虚拟机RAC的ASM磁盘组坏块导致重建DB
  • ASP.NET 5 入门 (2) – 自定义配置
  • hadoop进阶
  • 程序员编程艺术第一~二十二章集锦与总结(教你如何编程)
  • C语言的内存分配
  • 级别
  • 函数指针和指针函数
  • Oracle集合
  • [Vue CLI 3] 配置解析之 css.extract
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • conda常用的命令
  • JavaWeb(学习笔记二)
  • Java-详解HashMap
  • Laravel5.4 Queues队列学习
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • python docx文档转html页面
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • Sass Day-01
  • STAR法则
  • vue自定义指令实现v-tap插件
  • 工作手记之html2canvas使用概述
  • 京东美团研发面经
  • 面试遇到的一些题
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • 小而合理的前端理论:rscss和rsjs
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #Linux(Source Insight安装及工程建立)
  • ()、[]、{}、(())、[[]]命令替换
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (十)c52学习之旅-定时器实验
  • (转)c++ std::pair 与 std::make
  • (转载)Linux 多线程条件变量同步
  • .apk文件,IIS不支持下载解决
  • .mysql secret在哪_MySQL如何使用索引
  • .NET Core Web APi类库如何内嵌运行?
  • .NET/C# 项目如何优雅地设置条件编译符号?
  • .NET中 MVC 工厂模式浅析
  • /etc/motd and /etc/issue
  • /var/spool/postfix/maildrop 下有大量文件
  • @Async注解的坑,小心
  • [ 常用工具篇 ] AntSword 蚁剑安装及使用详解
  • [120_移动开发Android]008_android开发之Pull操作xml文件
  • [Angular] 笔记 7:模块