Accessing Database Using IronRuby

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.

You can use any OR/M in the .NET space to connect to databases.Writing
queries will be somewhat challenging though :slight_smile:

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

So you could use a ruby OR/M


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

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 A.” [email protected]

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 :slight_smile:

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

GitHub - jschementi/activerecord-mssql-adapter: ActiveRecord SQL Server Adapter

So you could use a ruby OR/M


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

Hi Jimmy,

Can I use the ActiveRecord SQL SERVER with Window Forms projects?

Thanks! I’ve added this to the IronRuby .NET documentation just as a
placeholder:

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

~js

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:

~js

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

Thanks!

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 S. <

Thanks, the change has been made.

From: [email protected]
[mailto:[email protected]] On Behalf Of Brannon J.
Sent: Wednesday, June 17, 2009 4:25 PM
To: [email protected]
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 S.
<[email protected]mailto:[email protected]>
wrote:
Thanks! I’ve added this to the IronRuby .NET documentation just as a
placeholder:

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

~js