Repository navigation
Expand file tree
/
Copy pathsample_ruby_win.rb
More file actions
42 lines (35 loc) · 1.43 KB
/
Copy pathsample_ruby_win.rb
File metadata and controls
42 lines (35 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#Using the TinyTDS driver
require 'tiny_tds'
require 'date'
#Connect to your database.
#Replace server name, username, and password with your credentials
client = TinyTds::Client.new username: 'username@servername', password: 'password',
host: 'servername', port: 1433,
database: 'databasename'
#SELECT
#Execute a simple select statement.
results = client.execute("SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer")
#Print results of select.
results.each do |row|
puts row
end
#INSERT
#Make sure you have followed the recommended settings for using TinyTDS with Azure
results = client.execute("SET ANSI_NULLS ON")
results = client.execute("SET CURSOR_CLOSE_ON_COMMIT OFF")
results = client.execute("SET ANSI_NULL_DFLT_ON ON")
results = client.execute("SET IMPLICIT_TRANSACTIONS OFF")
results = client.execute("SET ANSI_PADDING ON")
results = client.execute("SET QUOTED_IDENTIFIER ON")
results = client.execute("SET ANSI_WARNINGS ON")
results = client.execute("SET CONCAT_NULL_YIELDS_NULL ON")
#Configure date format to align with SQL Server datetime format
t = Time.now
curr_date = t.strftime("%Y-%m-%d %H:%M:%S.%L")
#Execute an insert statement
results = client.execute("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate)
OUTPUT INSERTED.ProductID VALUES ('SQL Server Express New', 'SQLEXPRESS New', 0, 0, '#{curr_date}' )")
#Print the ID of the inserted row.
results.each do |row|
puts row
end