一、使用Powershell插入数据到SQL中

  1、方法一

 $ServerName=xxx
$Database=xxx
$UserID=xxx
$Pwd=xxx
$conn=new-object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server=$ServerName;Database=$Database;User ID=$UserID;Password=$pwd;Trusted_Connection=False;Connect Timeout=6000"
$conn.ConnectionString=$ConnectionString
$conn.Open() $Query="insert into 表名称(字段名, EventTime, EventType, EventId, Message) values(数值,N'$EventTime',N'$EventType',N'$EventId',N'$Message')"
$cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=6000
$cmd.ExecuteNonQuery()
$conn.close()

  2、方法二

 $Database = "xxx"
$Server = "xxx"
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection $insert="insert into 表名称(字段名,EventTime, EventType,EventSource, EventId, Message,RecordId) values(数值,N'$EventTime',N'$EventType',N'$TaskDisplayName',N'$EventId',N'$Message',N'$RecordId')"
$cmd1=new-object system.Data.SqlClient.SqlCommand($insert,$Connection)
$cmd1.CommandTimeout=6000
$cmd1.ExecuteNonQuery()
$Connection.Close()

二、使用Powershell从数据库中获取数据

  

 $Database = "xxx"
$Server = "xxx"
$ServerName="xxx"
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
 
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100 $cmd=Invoke-Sqlcmd -ServerInstance $server -Database $Database -Query "select MAX(recordid) from [$Database].dbo.[$table] where [ServerName]='$serverName'"
$max=$cmd.column1

Powershell & SQL的更多相关文章

  1. powershell玩转SQL SERVER所有版本

    微软发布了最新的powershell for sql server 2016命令行客户端库.文章介绍了与之相关的实用方法. powershell 传教士 原创文章 2016-06-05, 2016-1 ...

  2. Win7 安装SQL SERVER 2012需要SP1补丁

    在操作系统Win7上安装SQL Server 2012时,报如下错误: 也就是说SQL Server 2012如要要安装在Windows 7 上,则至少需要安装SP1补丁.否则就会弹出上面提示信息.关 ...

  3. 安装 SQL Server 2012 的硬件和软件要求(官方全面)

    以下各节列出了安装和运行 SQL Server 2012 的最低硬件和软件要求. 有关 SharePoint 集成模式下 Analysis Services 的要求的详细信息,请参阅硬件和软件要求(S ...

  4. Linux 上的 SQL Server 2017 的安装指南

    一:介绍背景 微软在2016年 3 月首次对外宣布了 Linux 版的 SQL Server,并于2017年 7 月发布了首个公开 RC 版.前几日在美国奥兰多召开的微软 Ignite 2017 大会 ...

  5. 利用PowerShell监控Win-Server性能

    Q:如何系统层面的去监控一下Windows Server? A:额……一时间的话……能想到的可能也就是PowerShell+SQL Server+job,试试. 1.关于PowerShell 2.Po ...

  6. SharePoint咨询师之路:备份和恢复系列--制定备份计划

    本来想研究下如何做数据库服务器的集群,然而突然被同事问起如何在部署SharePoint服务场的时候做备份和恢复的计划,就先来复习和研究一下. 本系列包括: 备份服务器场和配置 备份web和服务应用程序 ...

  7. PowerShell_零基础自学课程_6_PS中获取帮助信息详解、管道、格式化输

    前些文章陆续的说了一些关于这些主题,但是讨论的都不够深入,今天我们深入的了解一下获取帮助信息.管道以及格式化输出的内容. 一.获取帮助信息 在PS中获取帮助信息,最常用的有: -? .get-comm ...

  8. Dynamics CRM 电子邮件服务器配置文件Advanced配置中关闭SSL

    在新建电子邮件服务器配置文件时Advanced中的Use SSL for Incoming/Outgoing Connection默认都是启用的而且无法编辑,启用SSL当然是为了安全的考虑,但当客户的 ...

  9. Miscellaneous Articles

    标记一下,慢慢看  http://www.oracle-base.com/articles/misc/articles-misc.php Miscellaneous Articles DBA Deve ...

随机推荐

  1. scrapy抓取的中文结果乱码解决办法

    使用scrapy抓取的结果,中文默认是Unicode,无法显示中文. 中文默认是Unicode,如: \u5317\u4eac\u5927\u5b66 在setting文件中设置: FEED_EXPO ...

  2. TomEE

    https://tomee.apache.org/ Apache TomEE, pronounced "Tommy", is an all-Apache Java EE 6 Web ...

  3. LFCS 系列第八讲:管理用户和用户组、文件权限和属性以及启用账户 sudo 访问权限

    由于 Linux 是一个多用户的操作系统(允许多个用户通过不同主机或者终端访问一个独立系统),因此你需要知道如何才能有效地管理用户:如何添加.编辑.禁用和删除用户账户,并赋予他们足以完成自身任务的必要 ...

  4. 解决错误: ios property has a previous declaration

    今天维护一个项目的时候,我新添加了一个库,运行的时候报错了: ios property has a previous declaration 上网查了一下没有找到思路,不过根据提示自己试了一下,原来这 ...

  5. FileZilla Server-Can’t access file错误解决方法

    在某服务器上用FileZilla Server搭建了一个FTP服务器.开始使用没有发现任何问题,后来在向服务器传送大文件的时候,发现总是传输到固定的百分比的时候出现 ”550 can’t access ...

  6. Unknown module(s) in QT: xlsx解决方法

    解决方法在此: https://github.com/dbzhang800/QtXlsxWriter Documentation: http://qtxlsx.debao.me QtXlsx is a ...

  7. dubbo配置约束

    此处主要记录dubbo配置的一些约束规则. 采用官网提供的原文,描述如下: 一.XML配置(官网原文) 以 timeout 为例: 方法级优先,接口级次之,全局配置再次之. 如果级别一样,则消费方优先 ...

  8. OSX 10.8+下开启Web共享的方法 /转

    OSX 10.8+ Mountain Lion 下开启 Web Sharing(Web 共享)的方法 JUL 28, 2012  #OS X  #how-to  #apache  #web  #sha ...

  9. linux常用指令--防火墙

    centos7 iptables :  如果你想使用iptables静态路由规则,那么就禁用centos7默认的firewalld,并安装ipteables-services, 启用iptables和 ...

  10. Python之Seaborn

    install: pip install seaborn official examples: https://seaborn.pydata.org/examples/index.html 在mac上 ...