Open Source RDBMS - Seamless, Scalable, Stable and Free

English | Login |Register

Using CUBRID with PowerShell

The scope of this tutorial is to show you how to work with CUBRID database from Windows PowerShell.

Overview

What is PowerShell?

Windows PowerShell 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.

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, download Powershell from Microsoft Official Website.

And if you need to read more and get familiar with PowerShell, the best place to start is Windows PowerShell Owner's Manual.

Setup

In order to be able to follow the steps in this tutorial, we will need the following prerequisites:

  • Make sure your system has .NET 3.5 or later.
  • Install CUBRID 8.4.0 (or later), if you don't have it already; choose to create the demodb database during installation.
  • Install PowerShell (see the link mentioned above).
  • Get the CUBRID ADO.NET Driver (CUBRID.Data.dll). You have the options to either:
  • 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.
  • Load the CUBRID OLED Data Provider in PowerShell. Launch the PowerShell console and then execute this command:

    [void][system.reflection.Assembly]::LoadFrom("C:\PowerShellCubrid.Data.dll")

    or the next one, if you will load it from GAC:

    [void][System.Reflection.Assembly]::LoadWithPartialName("Cubrid.Data")

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

Connecting to CUBRID

First, we need to know how to create the connection string we will use in our scripts.

The CUBRID connection string syntax to use is this:

"server=<server address>;database=<db. name>;port=<broker port>;user=<user ID>;password=<password>"

For example, to connect to the demodb database, we will use the following connection string:

$connectionString = "server=localhost;database=demodb;port=30000;user=dba;password=";

Let’s now write a complete script that opens a connection to the demodb database and close it immediately after:

$connectionString = "server=localhost;database=demodb;port=30000;user=dba;password=";
$connection = New-Object Cubrid.Data.CubridClient.CubridConnection
$connection.ConnectionString = $connectionString
$connection.Open()
Write-Host "Connected
to Cubrid!"
$connection.Close()

Now Copy-Paste the above script in the PowerShell console. If everything goes ok, you will get the successful execution message.

successful_execution.png

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:

  • Save your scripts using the extension ".ps1"
  • Execute in the PowerShell console the command: Set-ExecutionPolicy RemoteSigned and select Yes when prompted:
    Set-ExecutionPolicyRemoteSigned.png

  • Execute your scripts using the PowerShell the "Call" operator (the ampersand):

>& "C:\PowerShellScript01.ps1"

You can read more about executing scripts at http://technet.microsoft.com/en-us/library/ee176949.aspx

Retrieving some table data

Let’s write now a script which gets some records from the history table, in the demodb database and displays the records on screen, in the PowerShell console window.

For this, we will need to:

  • Define the SQL command to get the data:
$sql = "select * from history limit 0, 10"
  • Execute the SQL command and create the CubridCommand object:
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)
  • Define and populate a data reader object:
$dataReader = $command.ExecuteReader()
  • Iterate through the data and output records to the console:
while ($dataReader.Read())
{
  $row
= $row
+ "Code:      " + $dataReader.GetInt32(0)
+ "`n"

…

write-host $row "`n"
}

Note: As you can see, we are using the GetXXX(…) family of methods implemented in the CubridDataReader class.

After doing all these changes, we will have the following script:

[void][system.reflection.Assembly]::LoadFrom("C:\PowerShellCubrid.Data.dll")

$connectionString = "server=localhost;database=demodb;port=30000;user=dba;password=";
$connection = New-Object Cubrid.Data.CubridClient.CubridConnection  
$connection.ConnectionString = $connectionString  
$connection.Open()
$sql = "select * from history limit 0, 10"
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)
$dataReader = $command.ExecuteReader()
while ($dataReader.Read())
{ 
  $row = "===================================`n"
  $row = $row 
+ "Code:      " + $dataReader.GetInt32(0) 
+ "`n"
  $row = $row 
+ "Athlete:   " + $dataReader.GetString(1) + "`n 
"
  $row = $row 
+ "Host year: " + $dataReader.GetString(2) + "`n "
  $row = $row 
+ "Score:     " + $dataReader.GetString(3) 
+ "`n"
  $row = $row 
+ "Unit:      " + $dataReader.GetString(4)
  
write-host $row "`n"
}
$command.Close() 
$connection.Close()

Executing the script will output the data in the console window:

table_data.png

Executing DDL commands and using parameters

In the last example, we will:

  • Create a table
  • Populate it with some data
  • Retrieve the data
  • Drop the table

For creating the table, we will use the following definition:

CREATE TABLE ps(
	id INT NOT NULL,
    mydata VARCHAR(32),
    logtime TIMESTAMP
);

For executing a command which does not return a data set, we will use the ExecuteNonQuery() method.

As for inserting test data, we will use this time a SQL with parameters:

$sql = "INSERT INTO ps(id, mydata, logtime) VALUES(?, ?, ?)"

And here is the script that will do these operations:

[void][system.reflection.Assembly]::LoadFrom("C:\PowerShellCubrid.Data.dll")

#Define connection and connect
$connectionString = "server=localhost;database=demodb;port=30000;user=dba;password=";
$connection = New-Object Cubrid.Data.CubridClient.CubridConnection  
$connection.ConnectionString = $connectionString  
$connection.Open()
#Create table
$sql = "CREATE TABLE ps(id INT NOT NULL, mydata VARCHAR(32), logtime TIMESTAMP)"
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)
$command.ExecuteNonQuery();
$command.Close()
 
#Prepare data for insert

$sql = "INSERT INTO ps(id, mydata, logtime) VALUES(?, ?, ?)"
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)
 
#1st record
$command.Parameters.Add(1)
$command.Parameters[0].DbType = 11
$command.Parameters.Add("1st record")

$command.Parameters[1].DbType = 16
$mydate = New-Object DateTime(2010, 1, 1)
$command.Parameters.Add($mydate)
$command.Parameters[2].DbType = 6
#Insert data
$command.ExecuteNonQuery();

#2nd record

$command.Parameters[0].Value = 2
$command.Parameters[1].Value = "2nd record"
$mydate = New-Object DateTime(2010, 2, 2)
$command.Parameters[2].Value = $mydate;
#Insert data
$command.ExecuteNonQuery();
$command.Close()

#Retrieve data
$sql = "select * from ps"
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)
$dataReader = $command.ExecuteReader()
while ($dataReader.Read())
{ 
  $row = "`n"
  $row = $row + $dataReader.GetInt32(0)  + "`n"
  $row = $row + $dataReader.GetString(1) + "`n"
  $row = $row + $dataReader.GetString(2) + "`n"
  write-host $row "`n"
}
$command.Close()

#Drop table
$sql = "DROP TABLE ps"
$command = New-Object Cubrid.Data.CubridClient.CubridCommand($sql, $connection)
$command.ExecuteNonQuery();
$command.Close()
#Close connection
$connection.Close()

Look closer at the script source code and you will easy identify the purpose of each instruction.

Script.png

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.

Links & Resources

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.

See also

Introduction to CUBRID OLE DB Support

The scope of this tutorial is to provide an introduction to CUBRID OLE DB support. We will show you where and how to get the CUBRID OLE DB...




You are either using a very old browser or a browser that is not supported.
In order to browse cubrid.org you need to have one of the following browsers:



Internet Explorer: Mozilla Firefox: Google Chrome: