Forum: IronRuby Accessing Database Using IronRuby

Posted by Mohammad Azam (azamsharp)
on 2009-06-17 21:13
Hi,

I am trying to find a way to access database (SQL SERVER) using
IronRuby. One option is to use C# and ADO.NET classes to access the
database but was wondering if IronRuby has a build in support for
dynamic SQL so I don't have to deal with ADO.NET for database access.
Posted by Ivan Porto carrero (casualjim)
on 2009-06-17 21:27
(Received via mailing list)
You can use any OR/M in the .NET space to connect to databases.Writing
queries will be somewhat challenging though :)

Jimmy also wrote a sql server adapter for activerecord that comes with 
rails
so you could also use the rails OR/M ActiveRecord

http://github.com/jschementi/activerecord-mssql-adapter/tree/master

So you could use a ruby OR/M

---
Met vriendelijke groeten - Best regards - Salutations
Ivan Porto Carrero
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)
Posted by Jimmy Schementi (Guest)
on 2009-06-17 21:29
(Received via mailing list)
You could use the ActiveRecord SQLServer adapter ... Look for
documentation under the Rails section in ironruby.net/documentation.

~Jimmy
Sent from my phone

On Jun 17, 2009, at 12:13 PM, "Mohammad Azam" <lists@ruby-forum.com>
Posted by Mohammad Azam (azamsharp)
on 2009-06-17 21:29
Thanks! yeah I might be interested in using any .NET ORM!

I will try out Jimmy Rails ORM ActiveRecord. I think it will also work 
for a windows application.

Let me check out the dbi module.


Ivan Porto carrero wrote:
> You can use any OR/M in the .NET space to connect to databases.Writing
> queries will be somewhat challenging though :)
> 
> Jimmy also wrote a sql server adapter for activerecord that comes with 
> rails
> so you could also use the rails OR/M ActiveRecord
> 
> http://github.com/jschementi/activerecord-mssql-adapter/tree/master
> 
> So you could use a ruby OR/M
> 
> ---
> Met vriendelijke groeten - Best regards - Salutations
> Ivan Porto Carrero
> Blog: http://flanders.co.nz
> Twitter: http://twitter.com/casualjim
> Author of IronRuby in Action (http://manning.com/carrero)
Posted by Mohammad Azam (azamsharp)
on 2009-06-17 21:33
Hi Jimmy,

Can I use the ActiveRecord SQL SERVER with Window Forms projects?
Posted by Mohammad Azam (azamsharp)
on 2009-06-17 22:33
Just wanted to share basic code to insert first_name and last_name into 
the database.


require 'mscorlib'
require 'System.Data, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089'

SqlConnection = System::Data::SqlClient::SqlConnection
SqlCommand = System::Data::SqlClient::SqlCommand

class CustomerRepository

  def save(customer)

   connection = 
SqlConnection.new('Server=localhost;Database=TestDatabase;Trusted_Connection=true')
   connection.Open()
   command = SqlCommand.new("insert into customers(first_name,last_name) 
values(@first_name,@last_name)",connection)
   command.Parameters.AddWithValue("@first_name",customer.first_name)
   command.Parameters.AddWithValue("@last_name",customer.last_name)

   command.ExecuteNonQuery()
   connection.Close()

  end

end
Posted by Jimmy Schementi (Guest)
on 2009-06-17 23:29
(Received via mailing list)
Thanks! I've added this to the IronRuby .NET documentation just as a 
placeholder:

http://ironruby.net/Documentation/.NET/SQL

~js
Posted by Jimmy Schementi (Guest)
on 2009-06-18 01:16
(Received via mailing list)
Definitely, WinForms isn't anything special to restrict using SQLServer.

I actually show this briefly here:
http://blog.jimmy.schementi.com/2009/05/ironruby-at-railsconf-2009.html#sql

Source code is here:
http://github.com/jschementi/railsconf09/tree/master/8-ardb

~js
Posted by Brannon Jones (Guest)
on 2009-06-18 01:26
(Received via mailing list)
That code should use 'ensure', to make sure the connection is closed at 
the
end of the block:
require 'mscorlib'
require 'System.Data, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'

include System::Data::SqlClient

begin
connection =
SqlConnection.new('Server=localhost;Database=TestDatabase;Trusted_Connection=true')
connection.open
 command = SqlCommand.new(<<-EOS
  insert into customers(first_name,last_name)
  values(@first_name,@last_name)
EOS, connection)
 command.parameters.add_with_value "@first_name", "Jimmy"
command.parameters.add_with_value "@last_name", "Schementi"
 command.execute_non_query
ensure
connection.close unless connection.nil?
end


On Wed, Jun 17, 2009 at 14:03, Jimmy Schementi <
Posted by Mohammad Azam (azamsharp)
on 2009-06-18 02:43
Thanks!
Posted by Jimmy Schementi (Guest)
on 2009-06-24 00:55
(Received via mailing list)
Thanks, the change has been made.

From: ironruby-core-bounces@rubyforge.org 
[mailto:ironruby-core-bounces@rubyforge.org] On Behalf Of Brannon Jones
Sent: Wednesday, June 17, 2009 4:25 PM
To: ironruby-core@rubyforge.org
Subject: Re: [Ironruby-core] Accessing Database Using IronRuby

That code should use 'ensure', to make sure the connection is closed at 
the end of the block:

require 'mscorlib'
require 'System.Data, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089'

include System::Data::SqlClient

begin
          connection = 
SqlConnection.new('Server=localhost;Database=TestDatabase;Trusted_Connection=true')
          connection.open

          command = SqlCommand.new(<<-EOS
           insert into customers(first_name,last_name)
           values(@first_name,@last_name)
          EOS, connection)

          command.parameters.add_with_value "@first_name", "Jimmy"
          command.parameters.add_with_value "@last_name", "Schementi"

          command.execute_non_query
ensure
          connection.close unless connection.nil?
end


On Wed, Jun 17, 2009 at 14:03, Jimmy Schementi 
<Jimmy.Schementi@microsoft.com<mailto:Jimmy.Schementi@microsoft.com>> 
wrote:
Thanks! I've added this to the IronRuby .NET documentation just as a 
placeholder:

http://ironruby.net/Documentation/.NET/SQL

~js
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.