[Ironruby-core] Accessing Database Using IronRuby

Mohammad Azam lists at ruby-forum.com
Wed Jun 17 16:33:25 EDT 2009


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, at 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 via http://www.ruby-forum.com/.


More information about the Ironruby-core mailing list