Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. require 'tiny_tds'
  2. @client = TinyTds::Client.new username: 'sa', password: 'Yukon900',
  3. host: 'localhost', port: 1433
  4. puts 'Connecting to SQL Server'
  5.  
  6. if @client.active? == true then puts 'Done' end
  7.  
  8. def execute(sql)
  9. @client.execute(sql).do
  10. true
  11. end
  12.  
  13. # create database
  14. puts "Dropping and creating database 'SampleDB'"
  15. execute("DROP DATABASE IF EXISTS [SampleDB]; CREATE DATABASE [SampleDB];")
  16.  
  17. # create sample table with data
  18. puts "Creating sample table with data"
  19. puts ("USE SampleDB; CREATE TABLE Employees (Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, Name NVARCHAR(50), Location NVARCHAR(50))
  20. INSERT INTO Employees (Name, Location) VALUES (N'Jared', N'Australia'), (N'Nikita', N'India'), (N'Tom', N'Germany')")
  21.  
  22. # insert new employee
  23. puts "Inserting new employee Jake into Employees table"
  24. execute("INSERT INTO Employees (Name, Location) VALUES (N'Jake', N'United States')")
  25.  
  26.  
  27. # update location for employee
  28. puts "Updating Location for Nikita"
  29. execute("UPDATE Employees SET Location = N'United States' WHERE NAME = N'Nikita'")
  30.  
  31. # delete employee
  32. puts "Deleting employee Jared"
  33. execute("DELETE FROM Employees WHERE NAME = N'Jared'")
  34.  
  35. # read all employees
  36. puts "Reading data from table"
  37. @client.execute("SELECT * FROM Employees").each do |row|
  38. puts row
  39. end
  40.  
  41. puts "All done."
  42.  
  43. @client.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement