Tuesday, July 12, 2011

Insert many rows into MSSQL2005 using XML

This troubled me for a long time... Here's my solution in VB.net

                Dim xmlSource As New StringBuilder  
                dim recordsToCreate   
                recordsToCreate = 100000  
                for x = 1 to recordsToCreate   
                     xmlSource.Append("<row>")  
                          xmlSource.Append("<Col1>SomeData in col1 " & x & "</Col1>")  
                          xmlSource.Append("<Col2>SomeData in col2 " & x & "</Col2>")  
                     xmlSource.Append("</row>")  
                next   
                Dim startTime As Date = Now  
                ' Run the process that is to be timed.  
                Using con As New SqlConnection( connStr )  
                          con.Open()                           
                          strSQL = "DECLARE @xml XML; SET @xml=@xmlSource; "  
                          strSQL = strSQL & "INSERT INTO XDELETEME (col1, col2) Select "  
                          strSQL = strSQL & "Tbl.Col.value('Col1[1]', 'nvarchar(50)'), Tbl.Col.value('Col2[1]', 'nvarchar(50)') FROM @xml.nodes('//row') Tbl(Col) "  
                          cmd = New SqlCommand(strSQL, con)  
                          With cmd.Parameters  
                            .Add(New SqlParameter("@xmlSource", xmlSource.toString() ))  
                          End with  
          cmd.ExecuteNonQuery()  
                          con.Close()  
                End using  
                Dim runLength As Global.System.TimeSpan = Now.Subtract(startTime)  
                Dim millisecs As Integer = runLength.Milliseconds  
                response.write(recordsToCreate & " records imported in " & millisecs & " Milliseconds")  

This works really well for bulk inserts.

Sunday, May 1, 2011

Airplay through Media Center

So I felt like doing some media center programming this morning. I thought about how cool it would be to get AirPlay working with media center. Unfortunately, my solution is not native to MCE but it seems to work ok. I read this TUAW article about AirMediaPlayer, which opened to the door. AirMediaPlayer was discontinued by the author without releasing the source code, which SUCKS. So I wrote an AutoIt script to bridge the gaps between MCE and AirMediaPlayer. Here's a video of it in action:



How it works:

The first time you run the Airplay app from media center, it launches AirMediaPlayer and also launches a daemon to wait for the airplay connection, then returns you to media center. Now all you have to do is start playing a video on your iDevice, and click the airplay icon to select the computer name of your MCE machine. Once AirMediaPlayer receives the connection, MCE's "Now Playing" title is stopped, then MCE is minimized, and AirMediaPlayer is maximized to full screen mode. When you stop the video from the iDevice, AirMediaPlayer is minimized and you are returned to MCE. It's pretty seamless, although not native.

Setup: AirPlay.zip

1. So first download the zip file an extract the contents to "C:\Airplay\"

2. Run AirMediaPlayer. If you get an error, it's probably because you need to install Bonjour and Quicktime. If you can't get AirMediaPlayer to get airplay videos from your iphone, there is no point in going to Step 3 until you get that working. Once you can airplay videos, move on to step 3.

3. Run the install.cmd This will put the airplay app into media center. I only tested on windows 7 x64, so others will have to find a workaround. you can find the icon in the "Extras menu" or get Advent Media center studio to put it wherever you want it.

4. Launch the addin from media center, and you should see the screen flicker as AirMediaPlayer starts and MCE restores back.

5. Now launch a vid from your iphone, hit the airplay icon and send to MCE. MCE should notice and start the stream.

Other stuff:

There's a config.ini that you can edit that will enable logging or set the timeout of the daemon. The default timeout is 900 seconds (5 minutes). So if you dont send an airplay stream to MCE within that time period, AirMediaPlayer will close and you will have to click the MCE airplay icon again to start it.

Launcher.exe does all the real work. The source code is at launcher.au3 ( an autoit script ) feel free to recompile it if needed. If someone get this going on 32bit let me know and I'll add your changes.

So, that was my sunday... I would love to see Airplay audio, and a native Airplay video solution in the future. I just won't be the dev to do it, I dont have that kind of time anymore.