Skip to main content

Posts

SQL Generation

Recently I wrote a console application that generates SQL code to populate a database with sample data. I wanted it to be as generic as possible, but it still relies on a Source database to get sample data from. The application is configurable in that it lets you specify which Database is the source database, as well which tables to get the data out of. I did not want this application to depend on any stored procedures; it needs to be self contained, which is why I'm hard-coding the table definition sql query. So I call the console app via a batch file. The batch file calls the exe file, outputting the text to a file: 01: @ECHO OFF 02: bin\debug\SqlGenerator.exe > "C:\run.sql" 03: ECHO done. Here is the main method in the console app: 01: class Program { 02: static void Main( string [] args) { 03: Console.WriteLine(Database.GenerateSQL()); 04: } 05: } Here is the relevant excerpt from the App.config file: 01: < a
Recent posts

More DOS batch fun

Recently I needed to create a simple way to backup alot of website's log files. The directory structure is such: C:\DemoSites\ | |----\Demo1 | | | |-----\Logs | |----\Demo2 | | | |-----\Logs | |----\Demo3 | | | |-----\Logs | |----\Demo4 | | | |-----\Logs | ... etc ... So I need to backup the log files inside each "Logs" directory, in a loop... Here is the batch file that takes care of it: 01: @ECHO OFF 02:   03: :: Get Today's Date in YYYYMMDD Format... 04: for /F "tokens=2-4 delims=/ " %%a in ( 'date/t' ) do (set datevar=%%c%%a%%b) 05:   06: :: For each subdirectory of C:\DemoSites, Zip all log files in their Logs directories... 07: for /D %%d in (C:\DemoSites\*) do ( 08: if exist %%d\Logs ( 09: for /F "tokens=5 delims= " %%k in ( 'dir %%d\..\.' ) do ( 10: if exist %%d\..\%%k\Logs\*.* ( 11: if not exist

Backup Databases in a loop...

Note: This applies to Microsoft SQL Server 2005/2008...     Ever get sick of having a different SQL Job for each database backup? I created this method for backing up all databases (filtered) in a cursor. Just create a job with this as the Step and give the Job a schedule... 01: DECLARE @dbName varchar (50) 02: DECLARE @BKPDate varchar (10) 03: DECLARE @sql nvarchar (1000) 04: SET @BKPDate = replace ( convert ( varchar (10), getdate (), 102), '.' , '' ) 05: DECLARE bkpCursor CURSOR FOR 06: 07: /* only backup non-system databases */ 08: select [Name] from sys.databases where [owner_sid] <> 0x01 09: 10: OPEN bkpCursor 11: FETCH NEXT FROM bkpCursor INTO @dbName 12: IF @@FETCH_STATUS = 0 13: BEGIN 14: WHILE ( @@FETCH_STATUS = 0) 15: BEGIN 16: set @sql = 17: ' BACKUP DATABASE ' + @dbName + 18: ' TO

General practices (tomAYto tomAHto)...

I hate repeating code and sometimes generics can make things a bit easier. One of the things that needs to be done a lot with Drop-down lists is adding a "Please Select" as the first option, but I don't particularly like doing that in the data layer (stored procedure, etc.). I like to add that to the list, but why repeat the same code for every Drop-down list if there are several on the page? Here is one way I like to take care of that: Here are the supporting Classes: 01: #region Supporting Classes 02: public interface IReportObject { 03: int ID { get; set; } 04: string Name { get; set; } 05: } 06: public class CSP : IReportObject { 07: public int ID { get; set; } 08: public string Name { get; set; } 09: } 10: public class Base : IReportObject { 11: public int ID { get; set; } 12: public string Name { get; set; } 13: } 14: public class Property : IReportObject { 15: public int

Extend MVC HtmlHelper methods...

I always like to use helper methods, and the ASP.NET MVC HtmlHelper methods are great. But sometimes you need some extra logic that they don't provide... Enter custom extension methods. Here is a class I wrote specifically for extension methods ( Note: Some of the extension methods are calling some static Utility methods, and their implementation is not shown here ): 01: public static class HtmlHelperExtensions { 02:   03: public static string DateFormatterTextBox( this HtmlHelper htmlHelper, string name, 04: DateTime date, string format, object attributes) { 05: string val = string .Format(format, date); 06: if (date.Year == 1900) 07: val = "0000-00-00" ; 08: return htmlHelper.TextBox(name, val, attributes); 09: } 10: public static string DateFormatterTextBox( this HtmlHelper htmlHelper, string name, 11: DateTime date, string format) { 12: strin

Configure Microsoft SSRS to use a Web Service as a Data Source...

I recently created a SQL Server Reporting Services (SSRS) report which used a Stored Procedure as a datasource, but the report needed some extra customization we're getting by using PaxScript in a similar web page. This isn't easily achievable using SQL stored procedures, so I decided to use a web service instead as the data source for the report. This way I have the advantage of LINQ/C# and PaxScript. Also, we have a need to run different versions of the web service, each for a different client. So the data source's web service URL needs to be dynamic, rather than static. Here's how it's done... In the report Data Source properties, the Type is XML and the Connection string value is a Report Parameter: Next, the report's Dataset Text should be in the following format: Note: There is a Parameter for each Web Service method parameter with the correct Name and Type. The ElementPath value is derived partly from the WSDL and partly f

Tyler Eating

Here is a picture of Tyler eating Peas. They are all over his face and he loves it! If you look closely, you can see his first 2 teeth (bottom).