Skip to content

Instantly share code, notes, and snippets.

@mizanRahman
Last active December 11, 2015 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizanRahman/4537459 to your computer and use it in GitHub Desktop.
Save mizanRahman/4537459 to your computer and use it in GitHub Desktop.
fetching data from Microsoft SQL Server with ruby using TinyTDS gem
require 'tiny_tds'
client = TinyTds::Client.new(:username => 'sa', :password => 'secret', :host => 'localhosts')
client.execute("use testdb").do
#following block gets all the table names of the database and prints name.
result = client.execute("SELECT * FROM sys.Tables")
result.each do |row|
p row['name']
end
#following block get count of rows in a table.
result.do
result = client.execute("select count(*) as row_count from people")
first_row = result.first
p first_row['row_count']
File.open('result.txt', 'w') do |file|
file.write(first_row['row_count'])
end
# File.open('result.txt', 'w') do |file|
# result.each { |row| file.write(row['row_count']) }
# end
# result.each do |row|
# p row['row_count']
# # end
# result.do
# result = client.execute("select count(*) as row_count from people where age>20")
# row=result.each(:first => true)
# p row
client.close
@mizanRahman
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment