<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>Using CUBRID with PowerShell</title>
        <link>http://www.cubrid.org/?mid=using_cubrid_with_powershell</link>
        <description>Using CUBRID with PowerShell</description>
        <language>en</language>
        <pubDate>Fri, 08 Apr 2011 17:33:47 -0800</pubDate>
        <lastBuildDate>Wed, 15 Feb 2012 04:31:23 -0800</lastBuildDate>
        <generator>XpressEngine 1.4.4.1</generator>
                        										        <item>
            <title>Using CUBRID wit...</title>
            <dc:creator>CUBRID</dc:creator>
            <link>http://www.cubrid.org/using_cubrid_with_powershell</link>
            <guid isPermaLink="true">http://www.cubrid.org/using_cubrid_with_powershell</guid>
                                    <description><![CDATA[<h1>Using CUBRID with PowerShell</h1>

<div class="contents-table"></div>

<p>The scope of this tutorial is to show you how to work with CUBRID database from <b>Windows PowerShell</b>.</p>

<h2>Overview</h2>
 
<p>What is <b>PowerShell</b>?</p>
<p><a href="http://en.wikipedia.org/wiki/Windows_PowerShell">Windows PowerShell</a> is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework.</p>

<p>PowerShell is not installed by default on Windows XP and Vista OS, but it is included in Windows 7. If you don't have it yet on your system, <a href="http://blogs.msdn.com/b/powershell/archive/2008/12/30/download-windows-powershell.aspx">download Powershell</a> from Microsoft Official Website.</p>
 
<p>And if you need to read more and get familiar with PowerShell, the best place to start is <a href="http://technet.microsoft.com/en-us/library/ee221100.aspx">Windows PowerShell Owner's Manual</a>.</p>

<h2>Setup</h2>
 
<p>In order to be able to follow the steps in this tutorial, we will need the following prerequisites:</p>

<ul>
  <li>Make sure your system has <a href="http://www.microsoft.com/net/download">.NET 3.5</a> or later.</li>
  <li>Install CUBRID 8.4.0 (or later), if you don't have it already; choose to create the <b>demodb</b> database during installation.</li>
  <li>Install PowerShell (see the link mentioned above).</li>
  <li>Get the CUBRID ADO.NET Driver (CUBRID.Data.dll). You have the options to either:
  </li><ul><li><a href="/?mid=downloads&amp;item=ado_dot_net_driver&amp;os=windows" target="_self">Download the compiled library</a></li><li>Compile it yourself from the source code; you can checkout the code from the CUBRID SVN repository: <a href="http://svn.cubrid.org/cubridapis/adodotnet/branches/RB-8.4.1/" target="_self">http://svn.cubrid.org/cubridapis/adodotnet/branches/RB-8.4.1/</a></li></ul><li>Choose/Create a folder (for example, C:\PowerShell), where we will place and run the PowerShell scripts in this tutorial. Copy in that folder the CUBRID.Data.dll library.</li>
	<li>Load the CUBRID OLED Data Provider in PowerShell. Launch the PowerShell console and then execute this command:
  <p><code class="cmd block">[void][system.reflection.Assembly]::LoadFrom("C:\PowerShellCubrid.Data.dll")</code></p>
	<p>or the next one, if you will load it from GAC:</p>
	<p><code class="cmd block">[void][System.Reflection.Assembly]::LoadWithPartialName("Cubrid.Data")</code></p>
	</li>
</ul>

  
<p>After all these steps are completed, we are now ready to access CUBRID data from PowerShell.</p>

<h2>Connecting to CUBRID</h2>
 
<p>First, we need to know how to create the connection string we will use in our scripts.</p>
<p>The CUBRID connection string syntax to use is this:</p>

<div editor_component="code_highlighter" code_type="Plain" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
"server=&lt;server address&gt;;database=&lt;db. name&gt;;port=&lt;broker port&gt;;user=&lt;user ID&gt;;password=&lt;password&gt;"
</div>
 
<p>For example, to connect to the <b>demodb</b> 
database, we will use the following connection string:</p>
 
<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
$connectionString = "server=localhost;database=demodb;port=30000;user=dba;password=";
</div>
 
<p>Let’s now write a complete script that opens a connection to the demodb database and close it immediately after:</p>
 
<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
$connectionString = "server=localhost;database=demodb;port=30000;user=dba;password=";<br />
$connection = New-Object Cubrid.Data.CubridClient.CubridConnection<br />
$connection.ConnectionString = $connectionString<br />
$connection.Open()<br />
Write-Host "Connected<br />
to Cubrid!"<br />
$connection.Close()<br />
</div>
 
<p>Now Copy-Paste the above script in the PowerShell console. If everything goes ok, you will get the  successful execution message.</p><p><img src="http://www.cubrid.org/files/attach/images/236657/506/139/successful_execution.png" alt="successful_execution.png" width="606" height="135" editor_component="image_link"/>
<br /></p>
 
<p>Another alternative for executing scripts is to save them to a file and then load the file from the console. For this you need to:</p>

<ul>
  <li>Save your scripts using the extension "<b>.ps1</b>"</li>
  <li>Execute in the PowerShell console the command: <b>Set-ExecutionPolicy RemoteSigned</b> and select Yes when prompted:<br />
<img src="http://www.cubrid.org/files/attach/images/236657/506/139/Set-ExecutionPolicyRemoteSigned.png" alt="Set-ExecutionPolicyRemoteSigned.png" width="605" height="113" editor_component="image_link"/></li>
<br /><li>Execute your scripts using the PowerShell the "Call" operator (the ampersand):</li>
</ul>
  <p><code class="cmd block">&gt;&amp; "C:\PowerShellScript01.ps1"</code></p>

 
<p>You can read more about executing scripts at <a href="http://technet.microsoft.com/en-us/library/ee176949.aspx">http://technet.microsoft.com/en-us/library/ee176949.aspx</a> </p>

<h2>Retrieving some table data</h2>
 
<p>Let’s write now a script which gets some records from the <b>history</b> table, in the <b>demodb</b> database and displays the records on screen, in the PowerShell console window.</p>
 
<p>For this, we will need to:</p>
<ul>
  <li>Define the SQL command to get the data:</li>
</ul>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
$sql = "select * from history limit 0, 10"
</div>
 
<ul>
  <li>Execute the SQL command and create the CubridCommand object:</li>
</ul>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)
</div>
 
<ul>
  <li>Define and populate a data reader object:</li>
</ul>
 
<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
$dataReader = $command.ExecuteReader()
</div>

<ul>
  <li>Iterate through the data and output records to the console:</li>
</ul>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
while ($dataReader.Read())<br />
{<br />
  $row<br />
= $row<br />
+ "Code:      " + $dataReader.GetInt32(0)<br />
+ "`n"<br />
<br />
…<br />
<br />
write-host $row "`n"<br />
}<br />
</div>
 
<p class="important">Note: As you can see, we are using the GetXXX(…) family of methods implemented in the <b>CubridDataReader</b> class.</p>

<p>After doing all these changes, we will have the following script:</p>

<p><code class="cmd block">[void][system.reflection.Assembly]::LoadFrom("C:\PowerShellCubrid.Data.dll")</code></p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
$connectionString = "server=localhost;database=demodb;port=30000;user=dba;password=";<br />
$connection = New-Object Cubrid.Data.CubridClient.CubridConnection  <br />
$connection.ConnectionString = $connectionString  <br />
$connection.Open()<br />
$sql = "select * from history limit 0, 10"<br />
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)<br />
$dataReader = $command.ExecuteReader()<br />
while ($dataReader.Read())<br />
{ <br />
  $row = "===================================`n"<br />
  $row = $row <br />
+ "Code:      " + $dataReader.GetInt32(0) <br />
+ "`n"<br />
  $row = $row <br />
+ "Athlete:   " + $dataReader.GetString(1) + "`n <br />
"<br />
  $row = $row <br />
+ "Host year: " + $dataReader.GetString(2) + "`n "<br />
  $row = $row <br />
+ "Score:     " + $dataReader.GetString(3) <br />
+ "`n"<br />
  $row = $row <br />
+ "Unit:      " + $dataReader.GetString(4)<br />
  <br />
write-host $row "`n"<br />
}<br />
$command.Close() <br />
$connection.Close()<br />
</div>
  
<p>Executing the script will output the data in the console window:</p><p><img src="http://www.cubrid.org/files/attach/images/236657/506/139/table_data.png" alt="table_data.png" width="605" height="399" editor_component="image_link"/>
<br /></p>

<h2>Executing DDL commands and using parameters</h2>

<p>In the last example, we will:</p>
<ul>
  <li>Create a table</li>
  <li>Populate it with some data</li>
  <li>Retrieve the data</li>
  <li><a name="0.1__GoBack"></a>Drop the table</li>
</ul>
 
<p>For creating the table, we will use the following definition:</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
CREATE TABLE ps(<br />
	id INT NOT NULL,<br />
    mydata VARCHAR(32),<br />
    logtime TIMESTAMP<br />
);<br />
</div>
 
<p>For executing a command which does not return a data set, we will use the <b>ExecuteNonQuery()</b> method.</p>
<p>As for inserting test data, we will use this time a SQL with parameters:</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
$sql = "INSERT INTO ps(id, mydata, logtime) VALUES(?, ?, ?)"
</div>

<p>And here is the script that will do these operations:</p>
 
<p><code class="cmd block">[void][system.reflection.Assembly]::LoadFrom("C:\PowerShellCubrid.Data.dll")</code></p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px; background: url(&quot;modules/editor/components/code_highlighter/code.png&quot;) no-repeat scroll right top rgb(250, 250, 250);">
#Define connection and connect<br />
$connectionString = "server=localhost;database=demodb;port=30000;user=dba;password=";<br />
$connection = New-Object Cubrid.Data.CubridClient.CubridConnection  <br />
$connection.ConnectionString = $connectionString  <br />
$connection.Open()<br />
#Create table<br />
$sql = "CREATE TABLE ps(id INT NOT NULL, mydata VARCHAR(32), logtime TIMESTAMP)"<br />
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)<br />
$command.ExecuteNonQuery();<br />
$command.Close()<br />
 <br />
#Prepare data for insert<br />
<br />
$sql = "INSERT INTO ps(id, mydata, logtime) VALUES(?, ?, ?)"<br />
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)<br />
 <br />
#1st record<br />
$command.Parameters.Add(1)<br />
$command.Parameters[0].DbType = 11<br />
$command.Parameters.Add("1st record")<br />
<br />
$command.Parameters[1].DbType = 16<br />
$mydate = New-Object DateTime(2010, 1, 1)<br />
$command.Parameters.Add($mydate)<br />
$command.Parameters[2].DbType = 6<br />
#Insert data<br />
$command.ExecuteNonQuery();<br />
<br />
#2nd record<br />
<br />
$command.Parameters[0].Value = 2<br />
$command.Parameters[1].Value = "2nd record"<br />
$mydate = New-Object DateTime(2010, 2, 2)<br />
$command.Parameters[2].Value = $mydate;<br />
#Insert data<br />
$command.ExecuteNonQuery();<br />
$command.Close()<br />
<br />
#Retrieve data<br />
$sql = "select * from ps"<br />
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)<br />
$dataReader = $command.ExecuteReader()<br />
while ($dataReader.Read())<br />
{ <br />
  $row = "`n"<br />
  $row = $row + $dataReader.GetInt32(0)  + "`n"<br />
  $row = $row + $dataReader.GetString(1) + "`n"<br />
  $row = $row + $dataReader.GetString(2) + "`n"<br />
  write-host $row "`n"<br />
}<br />
$command.Close()<br />
<br />
#Drop table<br />
$sql = "DROP TABLE ps"<br />
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)<br />
$command.ExecuteNonQuery();<br />
$command.Close()<br />
#Close connection<br />
$connection.Close()<br />
</div>
 
<p>Look closer at the script source code and you will easy identify the purpose of each instruction.</p><p><img src="http://www.cubrid.org/files/attach/images/236657/506/139/Script.png" alt="Script.png" width="605" height="472" editor_component="image_link"/>
<br /></p>

<p>As you can see from the above examples, Windows PowerShell is a very powerful .NET based scripting language. PowerShell can access all of the .NET objects you need, in particular the objects that make up the implementation of the CUBRID OLE DB Data provider.</p>

<h2>Links &amp; Resources</h2>
 
<ul>
  <li><a href="http://blogs.msdn.com/b/powershell/">Windows PowerShell Blog</a></li>
  <li><a href="http://powershell.com/cs/">PowerShell resources</a></li>
  <li><a href="http://www.microsoft.com/download/en/details.aspx?amp;displaylang=en&amp;id=23407">Windows PowerShell 1.0 Documentation Pack</a></li>
  <li><a href="http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx">Scripting with Windows PowerShell</a></li><li><a href="/wiki_apis/entry/cubrid-ado-net-driver" target="_self">CUBRID ADO.NET Wiki</a></li>
</ul>

<p>This concludes the CUBRID PowerShell tutorial. Please let us know your feedback and remember to periodically check the CUBRID web site for other tutorials and resources.</p>]]></description>
                        <pubDate>Fri, 08 Apr 2011 17:16:41 -0800</pubDate>
                        <category>powershell</category>
                        <category>oledb</category>
                        <category>.net</category>
                        <category>tools</category>
                                </item>
            </channel>
</rss>
