#加载.net的winform模块
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$app=[System.Windows.Forms.Application]
$myForm=new-object System.Windows.Forms.Form
$myForm.Text="T_sql Excute window"

 
#执行程序的按钮

$button1 = new-object System.Windows.Forms.Button
$button1.Size = new-object System.Drawing.Size  -argumentlist 75, 23
$button1.Text = "执行程序"
$button1.Location = new-object System.Drawing.Point -argumentlist 200, 200

 
#获取第一个参数

$label1 = new-object System.Windows.Forms.Label;
$label1.Location = new-object System.Drawing.Point -argumentlist 1, 10
$label1.Size = new-object System.Drawing.Size  -argumentlist 80, 15
$label1.Text = "数据库地址 : "
$textbox1=new-object System.Windows.Forms.TextBox
$textBox1.Multiline = $true;
$textBox1.Text = ""
$textBox1.Size = new-object System.Drawing.Size  -argumentlist 150, 15
$textBox1.Location = new-object System.Drawing.Point -argumentlist 90, 10

#获取第二个参数
$label2 = new-object System.Windows.Forms.Label;
$label2.Location = new-object System.Drawing.Point -argumentlist 1, 50
$label2.Size = new-object System.Drawing.Size  -argumentlist 80, 15
$label2.Text = "用户名 : "
$textbox2=new-object System.Windows.Forms.TextBox
$textBox2.Multiline = $true;
$textBox2.Text = ""
$textBox2.Size = new-object System.Drawing.Size  -argumentlist 150, 15
$textBox2.Location = new-object System.Drawing.Point -argumentlist 90, 50

#获取第三个参数
$label3 = new-object System.Windows.Forms.Label;
$label3.Location = new-object System.Drawing.Point -argumentlist 1, 90
$label3.Size = new-object System.Drawing.Size  -argumentlist 80, 15
$label3.Text = "密码 : "
$textbox3=new-object System.Windows.Forms.TextBox
$textBox3.Multiline = $true;
$textBox3.Text = ""
$textBox3.Size = new-object System.Drawing.Size  -argumentlist 150, 15
$textBox3.Location = new-object System.Drawing.Point -argumentlist 90, 90

#获取第四个参数
$label4 = new-object System.Windows.Forms.Label;
$label4.Location = new-object System.Drawing.Point -argumentlist 1, 130
$label4.Size = new-object System.Drawing.Size  -argumentlist 80, 15
$label4.Text = "tsql路径 : "
$textbox4=new-object System.Windows.Forms.TextBox
$textBox4.Multiline = $true;
$textBox4.Text = ""
$textBox4.Size = new-object System.Drawing.Size  -argumentlist 150, 15
$textBox4.Location = new-object System.Drawing.Point -argumentlist 90, 130
# $textbox2=new-object System.Windows.Forms.TextBox
# $textBox2.Multiline = $true;
# $textBox2.Text = "test"
# $textBox2.Size = new-object System.Drawing.Size  -argumentlist 281, 113
#$flowLayoutPanel1 = new-object System.Windows.Forms.FlowLayoutPanel
$myForm.Controls.Add($label1)
$myForm.Controls.Add($textbox1)
$myForm.Controls.Add($label2)
$myForm.Controls.Add($textbox2)
$myForm.Controls.Add($label3)
$myForm.Controls.Add($textbox3)
$myForm.Controls.Add($label4)
$myForm.Controls.Add($textbox4)
$myForm.Controls.Add($button1)
#$myForm.Controls.Add($textbox2)
#$myForm.Controls.Add($label1)
#$flowLayoutPanel1.Controls.Add($label1);
#$flowLayoutPanel1.Controls.Add($textBox1);
#$flowLayoutPanel1.Controls.Add($textBox2);
#$flowLayoutPanel1.Controls.Add($button1);
#$flowLayoutPanel1.Dock = "Fill"
#$flowLayoutPanel1.FlowDirection = "TopDown"

#下面是批量执行tsql语句的函数
function exec_tsql{ 
         param( [string] $serverInstance ,
        [string] $userName ,
[string] $password ,
[string] $ScriptPath)

$ScriptList=""
[System.IO.DirectoryInfo]$DirectoryInfo=New-Object System.IO.DirectoryInfo $ScriptPath | Sort-Object
foreach( $f In ($DirectoryInfo.GetFiles("*.sql"))) 
  {
    $ScriptList=$ScriptList+";"+$f.Name
  }

Try
{
    #[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.ConnectionInfo') |out-null
    #$ServerConnection =new-object Microsoft.SqlServer.Management.Common.ServerConnection $serverInstance,$userName, $password
    Import-Module "sqlps" -DisableNameChecking
#try
    #{
        #$ServerConnection.BeginTransaction()
        #Write-Host "BeginTransaction ."
        
        [System.Text.StringBuilder]$Sql=""
        Foreach($File In $ScriptList.Split(";"))
        {        
            if($File -ne "")
            {
                #$Sql=$Sql.AppendLine(([System.Io.File]::OpenText($ScriptPath+$File)).ReadToEnd())
                #$ServerConnection.ExecuteNonQuery($Sql)|out-null
$fileurl = $ScriptPath + $File
Invoke-Sqlcmd -InputFile $fileurl  -ServerInstance $serverInstance -Database "Master" -Username $userName -Password $password 
                $Sql=""
                
                Write-Host $ScriptPath$File  " ...OK!"
            }
        }
        #$ServerConnection.CommitTransaction()
        
        #Write-Host "CommitTransaction ."      
    #}
    #Catch
    #{
    #    If ($ServerConnection.TransactionDepth -gt 0)
    #        {
    #            $ServerConnection.RollBackTransaction()
    #            Write-Host "RollBackTransaction ."
    #        }            
    #     
    #    Write-Error $_     
    #}    
}
Catch
{
    Write-Error $_
}
}
#这个函数到这里结束

#这里是执行按钮的事件,就是执行上面写的函数
$button1ClickEventHandler = [System.EventHandler] {
  $serverInstance = $textBox1.Text
  $userName = $textBox2.Text
  $password = $textBox3.Text
  $ScriptPath = $textBox4.Text
 exec_tsql $serverInstance $userName $password $ScriptPath
  [System.Windows.Forms.MessageBox]::Show("执行成功")
}
$button1.Add_Click($button1ClickEventHandler)
$app::EnableVisualStyles()
$app::Run($myForm)

powershell利用winform批量执行tsql语句的更多相关文章

  1. SQL*PLUS中批量执行SQL语句

    SQL*PLUS中批量执行SQL语句 今天由于工作的需要,要在CMD中批量执行大量的SQL语句,对于Oracle学习还处在入门阶段的我,只能硬着头皮到处去寻找资料(主要是网络资料,也包括自己的电子书) ...

  2. 批量执行sql语句

    基本使用 $sqls="sql语句1;sql语句2;sql语句n"; 或 $sqls="insert into xx;";  $sqls.="inse ...

  3. c#实现用SQL池(多线程),定时批量执行SQL语句 【转】

    在实际项目开发中,业务逻辑层的处理速度往往很快,特别是在开发Socket通信服务的时候,网络传输很快,但是一旦加上数据库操作,性能一落千丈,数据库操作的效率往往成为一个系统整体性能的瓶颈.面对这问题, ...

  4. PHP mysqli 增强 批量执行sql 语句的实现代码

    本篇文章介绍了,在PHP中 mysqli 增强 批量执行sql 语句的实现代码.需要的朋友参考下. mysqli 增强-批量执行sql 语句 <?php //mysqli 增强-批量执行sql ...

  5. c#实现用SQL池(多线程),定时批量执行SQL语句

    在实际项目开发中,业务逻辑层的处理速度往往很快,特别是在开发Socket通信服务的时候,网络传输很快,但是一旦加上数据库操作,性能一落千丈,数据库操作的效率往往成为一个系统整体性能的瓶颈.面对这问题, ...

  6. mysqli扩展库应用---批量执行sql语句

    1, mysqli批量执行sql语句.基本语法: $sqls=”sql1;sql2;………” mysqli::multi_query($sqls); 同一个$sqls要么是增删改语句集合,要么是查询语 ...

  7. JDBC连接MYSQL,批量执行SQL语句或在执行一个SQL语句之前执行一个SQL语句

    conn = MysqlJdbcUtils.getConnection(); Statement ps=conn.createStatement(); ps.addBatch("trunca ...

  8. Jmeter(三十八)Jmeter Question 之 ‘批量执行SQL语句’

    知识使我们变得玩世不恭,智慧使我们变得冷酷无情,我们思考的太多,感知太少,除了机器,我们更需要人性,除了智慧,我们需要仁慈和善良. ------出自查理卓别林的演讲 前面有提到Jmeter使用JDBC ...

  9. executeBatch()批量执行Sql语句

    executeBatch()方法:用于成批地执行SQL语句,但不能执行返回值是ResultSet结果集的SQL语句,而是直接执行stmt.executeBatch(); addBatch():向批处理 ...

随机推荐

  1. oracle 的变量的定义和赋值

    第一种 :先定义后赋值 代码 : declare     V_AgeingType        varchar2(500); begin V_AgeingType :='111'; end 第二种 ...

  2. Sniffer抓包教程

    上网络信息安全的时候用了下,中途出现了一堆奇葩的事,这里就不提了... 上教程: 先把虚拟机里面的防火墙给关了,主机防火墙也关了 之前由于ip自己设置了,然后一直ping不通,后面把ip改成自动获取就 ...

  3. SQL 插入查询的最大ID 号 进行批量

    INSERT INTO tbl_image_language ( code, docomo_cd, au_cd, softbank_cd ) SELECT DISTINCT((SELECT max(c ...

  4. Session深入理解

    Session是在什么情况下产生的 客户端访问服务器端,服务器端为每个用户生成一个唯一的sessionId,是这样吗?sessionId的作用是什么? http://www.cnblogs.com/s ...

  5. 重新开始学习javase_多态(动态绑定、推迟绑定或者运行期绑定)

    一,谈向上转换,或者上溯造型 什么是向上转换(上溯造型),一句话就是父类的引用指向子类的对象.或者把子类的对象当作父类来用 为什么要进行向上转换?我们先看一个例子吧! @Test public voi ...

  6. C++工厂方法模式

    class IFactoryBase { public: IFactoryBase(void); virtual ~IFactoryBase(void); public: virtual IProdu ...

  7. python正则表达式入门

    基本概念 使用正则表达式需要import re 表达式前加r避免转义 \d代表数字,\s代表空白字符,\w代表字母+数字. .代表任意单个字符 {m,n}代表前面字符至少出现m次,最多出现n次. (x ...

  8. css expression explaination

    http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx 据说已经被弃用的IE css写法,为了修复一些IE8及老版本 ...

  9. 你真的了解console吗?

    对于前端开发者来说,在开发过程中需要监控某些表达式或变量的值的时候,用 debugger 会显得过于笨重,取而代之则是会将值输出到控制台上方便调试.最常用的语句就是console.log(expres ...

  10. oracle触发器应用

    首先给大家推荐两篇我看后的博文,我已经内容转载过来: 1.对触发器的讲解 本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建 ...