PowerShell 小试
终于有一点时间,闲来无事。玩玩PowerShell。写了小段程序,
感觉PS还是不错的,唯一让人不爽的就是事件处理程序不能直接对
事件 += 来处理。废话不说了,把代码贴上。哈哈
#创建数据连接对象
$con = New-Object -TypeName System.Data.SqlClient.SqlConnection;
#设置连接字符串
$con.ConnectionString = "Data Source=.;Initial Catalog=HEMIS;Persist Security Info=false;User ID=sa;Password=123456";
#创建命令对象
$cmd = New-Object -TypeName System.Data.SqlClient.SqlCommand;
#设置命令对像属性
$cmd.Connection = $con;
$cmd.CommandText = "SELECT * FROM dbo.HEPer";
#创建数据适配器对象
$adpt = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter($cmd);
#创建数据表对象
$table = New-Object -TypeName System.Data.DataTable;
#填充数据
$adpt.Fill($table);
#遍历所有数据
foreach ($row in $table.Rows)
{
foreach($col in $table.Columns)
{
$col.ColumnName + “:” + $row[$col.ColumnName].ToString();
}
}
#创建窗体对象
$form = New-Object -TypeName System.Windows.Forms.Form;
#设置窗体属性
$form.Width = 600;
$form.Height = 400;
$form.Text = "PowerShell Demo";
#创建DataGridView对象
$grid = New-Object -TypeName System.Windows.Forms.DataGridView;
$grid.DataSource = $table;
$grid.Width = 400;
$grid.Height = 200;
$grid.Left = 100;
$grid.Top = 100;
#创建按钮对象
$button = New-Object -TypeName System.Windows.Forms.Button;
#设置按钮属性
$button.Text = "Close";
$button.Top = 20;
$button.Left = 100;
#将控件添加到窗体
$form.Controls.Add($grid);
$form.Controls.Add($button);
#设置按钮Click事件
$button.add_Click(
{
#打印事件参数sender
[System.Windows.Forms.MessageBox]::Show($this.ToString());
$this.Text = "closing";
#打印事件参数e
[System.Windows.Forms.MessageBox]::Show($_.ToString());
$form.Close();
}
);
#显示窗体
$form.ShowDialog();