this.Blog.Find(entry => entry.IsHelpful);
 Wednesday, April 30, 2008
Using Miado with Powershell

One of the nice things about Miado is that it wraps a lot of tedious, repetitive ADO.Net code and therefore greatly reduces the amount of code you actually do need to write. 

Here's a quick example.  One of the tasks I had to do at my last last gig was upload a set of prices from a CSV file every month.  I wrote a PowerShell script to use Miado to do the DB inserts:

  1. function BuildSql()   
  2. {   
  3.   # build the SQL   
  4.   $sb = New-Object System.Text.StringBuilder   
  5.   $sb.Append("insert into VendorPrice(VendorID, FixedPrice, VariablePrice, ");   
  6.   $sb.Append("CreationDate, LastUpdatedDate) ");   
  7.   $sb.Append("values(@VendorID, @FixedPrice, @VariablePrice, ");   
  8.   $sb.Append("current_timestamp, current_timestamp)");   
  9.     
  10.   return $sb.ToString();   
  11. }   
  12.   
  13. [Reflection.Assembly]::LoadFile("C:\dev\VS2008\Miado\src\Miado\bin\Release\Miado.dll")   
  14.   
  15. # setup the configuration   
  16. $connString = "Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabase;User Id=user;Password=passw0rd"  
  17. $dbProvider = System.Data.SqlClient.SqlClientFactory::Instance;
  18. $repository = Miado.MiadoRepositoryFactory::CreateMiadoRepository($dbProvider, $connString);
  19.   
  20. $sql = BuildSql;   
  21.   
  22. foreach ( $row in Import-Csv prices.csv | Select VendorID, FixedPrice, VariablePrice )    
  23. {  
  24.   $repository.ExecuteNonQuery($sql.ToString(), $row.VendorID, $row.FixedPrice, $row.VariablePrice);     
  25. }  

As you can see, the script iterates over each row in the CSV file and inserts a record for each row.  There's no need to write all the code necessary to create the DbConnection and DbCommand objects, nor do you have to dynamically create DbParameter objects for each row.  Just pass the SQL and the variables (in order of replacement) into the IMiadoRepository.ExecuteNonQuery() call and Miado takes care of all the ugly details.  One line for each insert - that's it!

Edit: This post was updated to reflect the changes in the 0.7.10530.1


Kick it on DotNetKicks.com
Wednesday, April 30, 2008 11:38:38 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  .Net | Miado | Powershell

 Tuesday, April 29, 2008
Apple to cut iPhone cost to under $200?

http://techland.blogs.fortune.cnn.com/2008/04/29/att-to-cut-the-price-of-apples-new-iphone/

Crazy if true.  Hell, it just might make it worthwhile to break a contract with Verizon ... 

And while we're on the subject - no wonder Motorola is going into the toilet - my effing 8 month old Razor can't hold a full charge for more than 12 hours with about 10 minutes of talk time.  wtf?!?!?!

Getting back to the iPhone.  Steve Jobs is sticking to his guns about Apple's goal of selling 10 million iPhones in its first year.  Well, this just might be the way to do it.  I think I hear the sound of a million first gen iPhones crying out in terror as the fanboys begin to line up for v2. 

The 2.0 upgrade due in June supposedly is also targeting corporate customers.  Hmm... I'm not convinced it will catch on in the enterprise space until they remove the requirement of using iTunes to provision it.  This just seems like a deal breaker to me.


Kick it on DotNetKicks.com
Tuesday, April 29, 2008 8:10:56 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  Apple | iPhone

Grrr!!!

I spent hours today trying to figure out why the hell my site was rendering all funky in IE while it looked just fine in Firefox.  In the end, it was merely a poorly placed embedded style="display: inline;" attribute on an element (well, two actually) in one blog post that was messing everything up in IE.


Kick it on DotNetKicks.com
Tuesday, April 29, 2008 5:39:55 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  Blog

 Tuesday, April 22, 2008
Getting the SysInternals Suite, Ruby style

John Robbins at Wintellect put out a nice blog post about using a PowerShell script to download and install the excellent suite of tools provided by Sysinternals.  I absolutely love the *concept* of PowerShell and the ability to harness the power of .Net at the command prompt.  The one downside to it is that I find the syntax to be a bit too verbose than what I normally expect out of a scripting language.  YMMV.

Anyway, in an effort to stay on top of my (limited) Ruby skills, I have ported his implementation to Ruby:

#!/usr/bin/env ruby

require 'net/http'
require 'zip/zip'

def usage
puts ""
puts "Downloads and extracts all the tools from Sysinternals"
puts ""
puts "Usage: ruby download_sysinternals.rb <extract-directory> [<save-directory>]"
puts "Required Parameter :"
puts " <extract-directory> : The directory where the SysinternalsSuite.zip"
puts " tools are extracted."
puts "Optional Parameters :"
puts " <save-directory> : Saves SysinternalsSuite.zip to the specified"
puts " directory. If not specified, the .ZIP file "
puts " is not saved."
puts " -? : Display this usage information"
puts ""
puts ""
Kernel#exit
end

def create_directory_if_needed(dir)
File.makedirs(dir) unless File.exist?(dir)
end

def download_sysinternals_zip_file(download_dir)
puts "Downloading SysinternalsSuite.zip file"
url = URI.parse('http://download.sysinternals.com/Files/SysinternalsSuite.zip')
req = Net::HTTP::Get.new(url.path)
resp = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
puts "Saving SysinternalsSuite.zip to: #{download_dir}"

file_name = "#{download_dir}/SysinternalsSuite.zip"
delete_file_if_exists(file_name)
open(file_name, "wb") { |file| file.write(resp.body) }

puts "Finished downloading"
end

def extract_zip_file(zip_file, extract_dir)
puts "Extracting zip file to: #{extract_dir}"
Zip::ZipFile.open(zip_file) do |zip|
zip.each do |entry|
file_name = "#{extract_dir}/#{entry.to_s}"
puts "Extracting #{file_name}"
# you will need to run this as an admin if
# you have sysinternals stuff in "Program Files"
delete_file_if_exists(file_name)
entry.extract(file_name)
end
end

puts "Finished extracting zip file"
end

def delete_file_if_exists(file_name)
File.delete(file_name) if File.exist?(file_name)
end

# here we go!!!
usage if ( ARGV.empty? || ARGV[0] == "-?" )

extract_dir = ARGV[0]
save_dir = ARGV.length > 1 ? ARGV[1] : ENV["TEMP"]

# download the file
create_directory_if_needed(save_dir)
download_sysinternals_zip_file(save_dir)

# extract the zip file
create_directory_if_needed(extract_dir)
zip_file = "#{save_dir}/SysinternalsSuite.zip"
extract_zip_file(zip_file, extract_dir)

# clean up after ourselves
delete_file_if_exists(zip_file) if ARGV.length < 1

Kick it on DotNetKicks.com
Tuesday, April 22, 2008 6:59:08 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  Powershell | Ruby | Sysinternals

 Wednesday, April 16, 2008
It's funny because it's true!

I just received this from Scott Hanselman's twitter feed (where he is attending the Microsoft MVP conference):

twitter: shanselman: "The UpdatePanel is like crack." - #mvp08

:-)


Kick it on DotNetKicks.com
Wednesday, April 16, 2008 12:37:53 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  .Net | Ajax | ASP.Net | Conferences | Humor

 Monday, April 14, 2008
New Release of Miado out on CodePlex

I have uploaded a new version of Miado out on CodePlex.  I am still calling this a beta.  There are a few features I would like to add before I officially proclaim a 1.0 version (ability to use SqlServer-type syntax with an ODBC provider and integrated caching with the IDbStatement are two at the top of my list).

For this latest version, I have moved the DbProvider and vendor specific stuff to the Miado.Configuration namespace.  I have added an IParameterParser interface which is used in the SimpleSql class to determine the parameter name declarations in a SQL statement based on whether you are using the ODBC-type syntax or newer syntax for variable declaration. For example,

MyParam = ?
vs.
MyParam = @MyParam

This week I am going to try and come up with a good set of DAOs that use the AdventureWorks DB so people can see samples of how the API should be used.


Kick it on DotNetKicks.com
Monday, April 14, 2008 6:52:06 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  .Net | Miado