{"id":33049,"date":"2010-06-09T08:15:34","date_gmt":"2010-06-09T07:15:34","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=33049"},"modified":"2010-06-09T11:12:33","modified_gmt":"2010-06-09T10:12:33","slug":"%e9%80%9a%e8%bf%87powershell%e6%93%8d%e4%bd%9csqlconnection-net%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33049","title":{"rendered":"\u901a\u8fc7PowerShell\u64cd\u4f5cSQLConnection .NET\u7c7b"},"content":{"rendered":"<p>.NET\u7c7bSystem.Data.SqlClient.SqlConnection\u7528\u4e8e\u5efa\u7acb\u5230SQL Server\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002\u7528\u8fc7\u6237\u53ef\u4ee5\u7528\u8fd9\u4e2a\u7c7b\u901a\u8fc7PowerShell\u8fde\u63a5\u5230SQL Server\uff0c\u67e5\u627e\u6570\u636e\u4ee5\u53ca\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u901a\u8fc7\u9ed8\u8ba4\u5b9e\u4f8b\u67e5\u8be2SQL Server\u7248\u672c\u53f7 <\/p>\n<blockquote>\n<p>#Let\u2019s set the location to the script folder C:\\Scripts  <\/p>\n<p>Set-Location C:\\Scripts  <\/p>\n<p>$SqlConnection = New-Object System.Data.SqlClient.SqlConnection  <\/p>\n<p>$SqlConnection.ConnectionString = &#8220;Server=WIN-SQL;Database=master;IntegratedSecurity=True&#8221;  <\/p>\n<p>$SqlCmd = New-Object System.Data.SqlClient.SqlCommand  <\/p>\n<p>$SqlCmd.CommandText = &#8220;Select @@version as SQLServerVersion&#8221;  <\/p>\n<p>$SqlCmd.Connection = $SqlConnection  <\/p>\n<p>$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter  <\/p>\n<p>$SqlAdapter.SelectCommand = $SqlCmd  <\/p>\n<p>$DataSet = New-Object System.Data.DataSet  <\/p>\n<p>$SqlAdapter.Fill($DataSet)  <\/p>\n<p>$SqlConnection.Close()  <\/p>\n<p>$DataSet.Tables[0]<\/p>\n<\/blockquote>\n<p>\u6267\u884c\u7ed3\u679c\u7c7b\u4f3c\u4e8e\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"powershellrecevied1\" style=\"border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px\" height=\"409\" alt=\"powershellrecevied1\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/06\/powershellrecevied1.png\" width=\"664\" border=\"0\"\/><\/p>\n<p>\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u56db\u4e2a\u7c7b\uff1a<\/p>\n<p>System.Data.SqlClient.SqlConnection<\/p>\n<p>System.Data.SqlClient.SqlCommand<\/p>\n<p>System.Data.SqlClient.SqlDataAdapter<\/p>\n<p>System.Data.DataSet<\/p>\n<p>SqlConnection\u7c7b\u7528\u4e8e\u521b\u5efa\u9488\u5bf9SQL Server\u6570\u636e\u5e93\u7684 \u8fde\u63a5\uff0cSQLCommand\u7c7b\u7528\u4e8e\u5bf9SQL Server\u6570\u636e\u5e93\u521b\u5efaTransact-SQL\u8bed\u53e5\u6216\u5b58\u50a8\u8fc7\u7a0b\uff0cSQLDataAdapter\u7c7b\u7528\u6237\u521b\u5efa\u4e00\u7cfb\u5217\u7528\u4e8e\u586b\u5145dataset\u7c7b\u7684\u6570\u636e\u547d\u4ee4\u548c\u6570\u636e\u5e93\u8fde\u63a5\u3002\u6570\u636e\u96c6\u4ee3\u8868\u5185\u5b58\u7f13\u5b58\u7684\u6570\u636e\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u811a\u672c\u9996\u5148\u901a\u8fc7WIN-SQL\u7684\u9ed8\u8ba4\u5b9e\u4f8b\u521b\u5efa\u8fde\u63a5\uff0c\u63a5\u4e0b\u6765\u521b\u5efa\u547d\u4ee4\u5bf9\u8c61\u5e76\u8bbe\u7f6eT-SQL\u8bed\u53e5&#8221;select @@version&#8221;\uff0c\u968f\u540e\u521b\u5efa\u6570\u636e\u9002\u914d\u5668\uff0c\u7528SqlCommand\u83b7\u5f97\u7684\u7ed3\u679c\u586b\u5145\u6570\u636e\u96c6\uff0c\u6700\u540e\u663e\u793a\u6570\u636e\u96c6\u3002<\/p>\n<p>\u540c\u6837\u4e5f\u80fd\u7528PowerShell\u901a\u8fc7\u7c7b\u4f3c\u7684.NET\u6846\u67b6\u7c7bSystem.Data.SqlClient.SqlConnection\u8fde\u63a5\u5230SQL Server\u547d\u540d\u5b9e\u4f8b\uff0c\u5982\u4e0b\uff1a<\/p>\n<blockquote>\n<p>$SqlConnection = New-Object System.Data.SqlClient.SqlConnection  <\/p>\n<p>$SqlConnection.ConnectionString = &#8220;Server=WIN-SQL;Database=master;  <\/p>\n<p>Integrated Security=True&#8221;  <\/p>\n<p>$SqlCmd = New-Object System.Data.SqlClient.SqlCommand  <\/p>\n<p>$SqlCmd.CommandText = &#8220;select @@version as SQLServerVersion&#8221;  <\/p>\n<p>$SqlCmd.Connection = $SqlConnection  <\/p>\n<p>$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter  <\/p>\n<p>$SqlAdapter.SelectCommand = $SqlCmd  <\/p>\n<p>$DataSet = New-Object System.Data.DataSet  <\/p>\n<p>$SqlAdapter.Fill($DataSet)  <\/p>\n<p>$SqlConnection.Close()  <\/p>\n<p>$DataSet.Tables[0]<\/p>\n<\/blockquote>\n<p>\u9700\u8981\u5f3a\u8c03\u7684\u662fWIN-SQL\u662f\u4e3b\u673a\u540d\u3002<\/p>\n<p>T-SQL\u547d\u4ee4\u201dselect @@version as SQLServerVersion\u201d\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a <br \/><img loading=\"lazy\" decoding=\"async\" title=\"powershellrecevied2\" style=\"border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px\" height=\"387\" alt=\"powershellrecevied2\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/06\/powershellrecevied2.png\" width=\"640\" border=\"0\"\/> <\/p>\n<p>\u7528\u6237\u4e5f\u5757\u53ef\u4ee5\u7528PowerShell\u901a\u8fc7\u540c\u540d\u7684.NET\u6846\u67b6\u7c7b\u5e93System.Data.SqlClient.SqlConnection\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b\u5e76\u901a\u8fc7SQL Server\u547d\u540d\u5b9e\u4f8b\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<blockquote>\n<p>$SqlConnection = New-Object System.Data.SqlClient.SqlConnection  <\/p>\n<p>$SqlConnection.ConnectionString = &#8220;Server=WIN-SQL;Database=master; Integrated Security=True&#8221;  <\/p>\n<p>$SqlCmd = New-Object System.Data.SqlClient.SqlCommand  <\/p>\n<p>$SqlCmd.CommandText = &#8220;sp_who&#8221;  <\/p>\n<p>$SqlCmd.Connection = $SqlConnection  <\/p>\n<p>$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter  <\/p>\n<p>$SqlAdapter.SelectCommand = $SqlCmd  <\/p>\n<p>$DataSet = New-Object System.Data.DataSet  <\/p>\n<p>$SqlAdapter.Fill($DataSet)  <\/p>\n<p>$SqlConnection.Close()  <\/p>\n<p>$DataSet.Tables[0]<\/p>\n<\/blockquote>\n<p>\u80fd\u591f\u770b\u5230\u6267\u884c\u5b58\u50a8\u8fc7\u7a0bsp_who\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<pre><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"powershellrecevied3\" style=\"border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px\" height=\"423\" alt=\"powershellrecevied3\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/06\/powershellrecevied3.png\" width=\"640\" border=\"0\"\/> <\/p>\n<p>\u56e0\u4e3a\u5f88\u591a\u65f6\u5019\u7528\u6237\u9700\u8981\u77e5\u9053\u5df2\u5b89\u88c5\u7684SQL Server\u7684\u7248\u672c\uff0c\u4e0b\u9762\u4f1a\u521b\u5efa\u540d\u4e3aGet-SQLVersion.ps1\u7684\u811a\u672c\uff0c\u80fd\u591f\u7528\u4e8e\u8fd4\u56de\u4efb\u4f55SQL Server\u5b9e\u4f8b\u7684\u7248\u672c\u53f7\uff0c\u4ee5\u4e0b\u662f\u811a\u672c\u7684\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n<blockquote>\n<p>Param (\n<\/p>\n<p>[string] $SQLSERVER\n<\/p>\n<p>)\n<\/p>\n<p>$SqlConnection = New-Object System.Data.SqlClient.SqlConnection\n<\/p>\n<p>$SqlConnection.ConnectionString = &#8220;Server=$SQLSERVER;Database=master;Integrated\n<\/p>\n<p>Security=True&#8221;\n<\/p>\n<p>$SqlCmd = New-Object System.Data.SqlClient.SqlCommand\n<\/p>\n<p>$SqlCmd.CommandText = &#8220;Select @@version as SQLServerVersion&#8221;\n<\/p>\n<p>$SqlCmd.Connection = $SqlConnection\n<\/p>\n<p>$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter\n<\/p>\n<p>$SqlAdapter.SelectCommand = $SqlCmd\n<\/p>\n<p>$DataSet = New-Object System.Data.DataSet\n<\/p>\n<p>$SqlAdapter.Fill($DataSet)\n<\/p>\n<p>$SqlConnection.Close()\n<\/p>\n<p>$DataSet.Tables[0]<\/p>\n<\/blockquote>\n<p>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012SQL Server\u5b9e\u4f8b\u540d\u7684\u65b9\u5f0f\u8c03\u7528\u8be5\u811a\u672c\uff0c\u5982\uff1a<\/p>\n<p>.\\Get-SQLVersion.ps1 &#8220;WIN-SQL&#8221;<\/p>\n<p>.\\Get-SQLVersion.ps1 &#8220;WIN-SQL\\SQL2008&#8221;<\/p>\n<p>\u4e0b\u56fe\u662f\u201dselect @@verson\u201d\u547d\u4ee4\u7684\u8fd4\u56de\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"powershellrecevied4\" style=\"border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px\" height=\"389\" alt=\"powershellrecevied4\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/06\/powershellrecevied4.png\" width=\"640\" border=\"0\"\/> <\/p>\n<p>\u672c\u6587\u901a\u8fc7SqlConnection\u8fd9\u4e2a.NET\u7c7b\u8fde\u63a5SQL Server\u4ee5\u53ca\u8bbf\u95eeSQL Server\u6570\u636e\u5e93\u7684\u65b9\u6cd5\uff0c\u9664\u6b64\u4ee5\u5916\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u83b7\u53d6\u6570\u636e\u90a3\u5c31\u662fSQL SMO\uff0c\u540e\u7eed\u7684\u6587\u7ae0\u5c06\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<p>\u4f5c\u8005: \u4ed8\u6d77\u519b <br \/>\u7248\u6743\uff1a\u672c\u6587\u7248\u6743\u5f52\u4f5c\u8005\u6240\u6709 <br \/>\u8f6c\u8f7d\uff1a\u6b22\u8fce\u8f6c\u8f7d\uff0c\u4e3a\u4e86\u4fdd\u5b58\u4f5c\u8005\u7684\u521b\u4f5c\u70ed\u60c5\uff0c\u8bf7\u6309\u8981\u6c42\u3010\u8f6c\u8f7d\u3011\uff0c\u8c22\u8c22 <br \/>\u8981\u6c42\uff1a\u672a\u7ecf\u4f5c\u8005\u540c\u610f\uff0c\u5fc5\u987b\u4fdd\u7559\u6b64\u6bb5\u58f0\u660e\uff1b\u5fc5\u987b\u5728\u6587\u7ae0\u4e2d\u7ed9\u51fa\u539f\u6587\u8fde\u63a5\uff1b\u5426\u5219\u5fc5\u7a76\u6cd5\u5f8b\u8d23\u4efb <br \/>\u4e2a\u4eba\u7f51\u7ad9: http:\/\/txj.shell.tor.hu<\/p>\n","protected":false},"excerpt":{"rendered":"<p>.NET\u7c7bSystem.Data.SqlClient.SqlConnection\u7528\u4e8e\u5efa\u7acb\u5230SQL Server [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,1],"tags":[],"class_list":["post-33049","post","type-post","status-publish","format-standard","hentry","category-powershell","category-default"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33049"}],"collection":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33049"}],"version-history":[{"count":2,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33049\/revisions"}],"predecessor-version":[{"id":33053,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33049\/revisions\/33053"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}