转至:http://www.pstips.net/connect-sql-database.html

PowerShell 通过ADO.NET连接SQL Server数据库,并执行SQL脚本。工作中整理的一小段脚本,后来没有用上,先记录在这里:

  1. 建立数据库连接
  2. 查询返回一个DataTatble对象
  3. 执行一条SQL语句
  4. 通过事物执行多条SQL语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# 建立数据库连接.
#
function New-SqlConnection([string]$connectionStr)
{
    $SqlConnection New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = $connectionStr
    try{
        $SqlConnection.Open()
        Write-Host 'Connected to sql server.'
        return $SqlConnection
    }
    catch [exception] {
        Write-Warning ('Connect to database failed with error message:{0}' -f ,$_)
        $SqlConnection.Dispose()
        return $null
    }
}
 
#
# 查询返回一个DataTable对象
#
function Get-SqlDataTable
{
    param
    (
    [System.Data.SqlClient.SqlConnection]$SqlConnection,
    [string]$query
    )
    $dataSet new-object "System.Data.DataSet" "WrestlersDataset"
    $dataAdapter new-object "System.Data.SqlClient.SqlDataAdapter" ($query,$SqlConnection)
    $dataAdapter.Fill($dataSet) | Out-Null
    return $dataSet.Tables | select -First 1
}
 
#
# 执行一条SQL命令
#
function Execute-SqlCommandNonQuery
{
    param
    (
    [System.Data.SqlClient.SqlConnection]$SqlConnection,
    [string]$Command
    )
    $cmd $SqlConnection.CreateCommand()
    try
    {
        $cmd.CommandText = $Command
        $cmd.ExecuteNonQuery() | Out-Null
        return $true
    }
    catch [Exception] {
         Write-Warning ('Execute Sql command failed with error message:{0}' -f $_)
         return $false
    }
    finally{
        $SqlConnection.Close()
    }
}
 
#
# 通过事物处理执行多条SQL命令
#
function Execute-SqlCommandsNonQuery
{
    param
    (
    [System.Data.SqlClient.SqlConnection]$SqlConnection,
    [string[]]$Commands
    )
    $transaction $SqlConnection.BeginTransaction()
    $command $SqlConnection.CreateCommand()
    $command.Transaction = $transaction
    try
    {
        foreach($cmd in $Commands) {
            #Write-Host  $cmd -ForegroundColor Blue
            $command.CommandText = $cmd
            $command.ExecuteNonQuery()
        }
        $transaction.Commit()
        return $true
    }
    catch [Exception] {
         $transaction.Rollback()
         Write-Warning ('Execute Sql commands failed with error message:{0}' -f $_)
         return $false
    }
    finally{
        $SqlConnection.Close()
    }
}

【转】PowerShell 连接SQL Server 数据库 - ADO.NET的更多相关文章

  1. JDBC连接SQL server与ADO.NET连接Sql Server对比

    JDBC连接SQL server与ADO.NET连接Sql Server对比 1.JDBC连接SQL server 1)java方面目前有很多驱动能够驱动连接SQL servernet.   主流的有 ...

  2. Excel VBA 连接各种数据库(三) VBA连接SQL Server数据库

    本文主要涉及: VBA中的SQL Server环境配置 VBA连接SQL Server数据库 VBA读写SQL Server数据 如何安装SQL Client 系统环境: Windows 7 64bi ...

  3. python 使用pymssql连接sql server数据库

    python 使用pymssql连接sql server数据库   #coding=utf-8 #!/usr/bin/env python#------------------------------ ...

  4. NetBeans连接SQL server数据库教程

    不废话,直接开始 1.下载sqljdbc.jar 可以从微软中国官方网站下载 SQLJDBC微软中国 笔者提供一个网盘链接Sqljdbc.jar 4个压缩包视版本选择,SQL 2012 用sqljdb ...

  5. JDBC连接sql server数据库及其它

    JDBC连接sql server数据库的步骤如下: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的 ...

  6. ThinkPHP连接sql server数据库

    亲身经历,在网上找连接sql server数据库的方法,还是不好找的,大多数都是照抄一个人的,而这个人的又写的不全,呵呵,先介绍一下我连接的方法吧.如果你是用THINKPHP连接,那么最重要的就是配置 ...

  7. JDBC连接sql server数据库的详细步骤和代码

    JDBC连接sql server数据库的详细步骤和代码 JDBC连接sql server数据库的步骤如下: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Ja ...

  8. python连接sql server数据库实现增删改查

    简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...

  9. 详解连接SQL Server数据库的方法,并使用Statement接口实现对数据库的增删改操作

    总结一下,连接SQL Server数据库需要以下几个步骤: 1. 导入驱动Jar包:sqljdbc.jar 2. 加载并注册驱动程序 3. 设置连接路径 4. 加载并注册驱动 5. 连接数据库 6. ...

随机推荐

  1. *HDU1325 并查集

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. mysql 查询去重 distinct

    mysql 查询去重 distinct   待完善内容..

  3. BSBuDeJie_03

    一 快速登录 1 改变状态栏的style - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightConte ...

  4. SQL21日自学通笔记(1)

    查找不重复数据 DISTINCT 查询日期 在access中格式是yyyy-mm-dd,Query执行的语句中用‘#’+Formatdata(‘yyyy-mm-dd’,date)+‘#’ SQL运算符 ...

  5. javascript函数小练习

    求n-m之间数据的和 <script> function num(n,m){ var sum=0; for (var i = n; i <= m; i++) { sum+=i; } ...

  6. swif-throws异常抛出

    import UIKit enum VendingMachineError: Error { case invalidSelection //选择无效 case insufficientFunds(c ...

  7. 1JavaScript简介

    文档对象模型(DOM,Document Object Model)是针对XML但经过扩展用于HTML的应用程序编程接口(API,Application Programming Interface). ...

  8. SQL TOP 子句、SQL LIKE 操作符、SQL 通配符

    TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. SQL Server 的语法: S ...

  9. jquery复习笔记

    Jquery基础 让一个按钮灰掉 $("button").("disabled","true"); ance desc选择器(ance代表祖 ...

  10. 前台获取Dropdownlist选中的text

    $("#ddltest").find("option:selected").text()