Thursday, April 18, 2013

Creating first Android app using C# / Dot42

First, you have to download the Dot42 setup from https://www.dot42.com/download.aspx.  The community license is free for a single developer and free apps can be developed using the community license.  For commercial purpose, you have to purchase the professional license.

Install the Dot42Setup.exe and follow the steps to complete the installation:
























Now, open the Visual Studio 2010 and create a new project using Dot42 template.  Select dot42 Applicatino project and provide a application name.



Once you click on OK, you have to select the Target framework version along with the certificate.  While creating the application for the first time, you have to select the  icon and create a new certificate.












Once you click on Ok, the new project gets created with two files (MainActivity.cs and MainLayout.xml which is the layout xml file).






















By default, a TextView is created with the default Hello World as the text.  Now, let us add a new button and on click of the button, we can show an dialog.  First, we have to add the button in the Layout xml file.


  <Button
      android:id="@+id/button"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Show Alert"/>

In the OnCreate method, we have to identify the button in the layout file first and add event handler to it.

        var button = FindViewById<Button>(R.Ids.button);

        button.Click += ShowOnClick;

Now, we have to create the method ShowOnClick and write logic to show the alert dialog.


            var builder = new AlertDialog.Builder(this);
            builder.SetMessage("Test alert dialog !");
            var dialog = builder.Create();
            dialog.Show();

Now, the MainActivity.cs file looks like below:














Okay, it's time now to debug and deploy the application into the mobile device.  I'm using HTC explorer mobile for deploying this test application.  Since HTC Explorer runs on Andorid 2.3 version, I'm changing the target version to 2.3 from Project -> Properties -> Android -> Android Version -> 2.1.

I have installed HTC Sync application in my local machine and connected my mobile using USB port / HTC Sync option.

Now, press F5 to deploy the application.  Once you press F5, the device connected to the USB port gets listed in the list.  I'm selecting the HTC mobile and clicking on Ok.




























The application is deployed into the mobile device successfully.

Clicking on the button on the application, generates the alert dialog like below.














That's it :) .... I have created my first Android app in less than 10 minutes.

Wednesday, April 17, 2013

Android application development using C#

I wanted to develop some apps for Android platform and primarily being a .net developer, I was thinking of switching to Java platform for developing Android applications.

Then, I thought of evaluating alternate options to develop Android apps using C# itself.  I came across the below options.

1. Dot42
    http://www.dot42.com/

2. Mono Project / Xamarin
    http://www.mono-project.com/Main_Page
    http://xamarin.com/

I started evaluating Dot42 and found as a cool option to build Android apps using Visual Studio 2010 itself.   Dot42 doesn't require any runtime environment (like Mono) and we can debug and deploy the application into Android devices from Visual Studio itself.

The API names are exactly same with the DLLs provided by Dot42.  If we know the method name in Java for any functionality, we can use method name (only casing is the difference) in C#.

I was able to create my first test application using C# in less than 10 minutes.  I will share in my next blog on how to create the test app using Dot42.

Tuesday, April 16, 2013

Iterating through a SQL Server script using C#

I hope you would have read my previous blog on how to parse the SQL Server script using C#.

Once the script is parsed successfully, the entire script is available in TSqlScript object.  A TSqlScript can contain multiple TSqlBatch objects.  Each TSqlBatch object contains multiple TSqlStatement objects to it.

For each of the DDL/DML statements, there is a class associated to it (say SelectStatement, DeleteStatement, UpdateStatement etc.,).  All these classes are inherited from the main abstract class TSqlStatement.


Sunday, April 14, 2013

SQL Analyser - Conceptualization

I have been using FxCop and StyleCop tools to get the static code analysis report for C# code to improve the code quality.  Sometime back, I started looking for similar tools which can do the static code analysis for the database code written in SQL Server.  Surprisingly, I couldn't figure out any tools available in the market. All I have found are the below 14 rules suggested by Microsoft which can be configured within the Visual Studio.

http://msdn.microsoft.com/en-us/library/dd193411(v=vs.90) – Design rules
http://msdn.microsoft.com/en-us/library/dd172117(v=vs.90) – Performance rules
http://msdn.microsoft.com/en-us/library/dd193246(v=vs.90) – Naming rules

After going through the above URL's, I realized that the 14 rules are very limited and there is a lot of scope to add more rules to the list.  I have gone through the MSDN forums and it seems Microsoft is not going to incorporate any static code analysis tools in the next version / near future.

With that, I started exploring how I can build my own tool which can take DB scripts as input, evaluate the script against the set of rules and generate a report with any violations.

After doing lot of googling, I identified how the SQL files are parsed in SSMS.  SSMS uses a set of .net libraries for parsing the scripts written in SQL Server.  Please read http://daravind.blogspot.in/2013/04/parsing-sql-server-script-using-c.html to know more details about that.  I wanted to leverage the same set of libraries and started building a POC to incorporate the 14 existing rules defined by Microsoft.

With the POC, I spent a good amount of time with my peers and started looking at how we can build this as a product which can be taken to the market.   Lots of great ideas came out of the discussion from my peers and we started working on building this product.  We came up the list of 60+ rules which can be validated against each and every script uploaded to the tool.  The user will have the flexibility to select the rules against which the script can be validated.

We built the first version of the tool as a windows application and later we wanted to expose the same as a Web application / WCF service and would like to build a plug-in for Visual Studio and SSMS.  We have successfully completed the initial version of the web application.  The trial version of the application can be evaluated from http://sqlanalyser.happiestminds.com with a simple registration.

In a nutshell, SQL Analyzer is code analysis tool for Microsoft SQL Server which evaluates SQL scripts written in SQL Server against the best practices provided by Microsoft and Happiest Minds practices and provides recommendations for improvement.  SQL Analyzer works as both static and dynamic code analysis tool. SQL Analyzer identifies the defects in early development cycle. It helps you to release the quality of database code to production and saves the cost of fixing the defect late in production cycle.  

With new ideas coming in, the product is evolving and now we are focusing on how to incorporate dynamic code analysis by connecting toe the database and fetch the scripts.

Please read the below blogs written by my peers on the tool:

http://dattatreyakulkarni.blogspot.in/2012/09/tool-based-ms-sql-script-review-for.html

http://dattatreyakulkarni.blogspot.in/2012/09/sql-analyzer-part-ii.html

http://architasblog.blogspot.in/2013/01/sql-analyzer.html

Will write more in coming blogs about the usage and the different rules incorporated in the tool ....

Friday, April 12, 2013

Parsing SQL Server script using C#

To parse the SQL Server script using C#, the following DLL's have to be referenced in the project.

Microsoft.Data.Schema.ScriptDom.dll
Microsoft.Data.Schema.ScriptDom.Sql.dll

Once you have Visual Studio 2010 installed in your machine, the above DLL's would be available at C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\

Once the above DLL's are referenced in the project, the below namespaces have to be included:

using Microsoft.Data.Schema.ScriptDom.Sql;
using Microsoft.Data.Schema.ScriptDom;

TSql100Parser class available in Microsoft.Data.Schema.ScriptDom.Sql namespace can be used to parse the SQL scripts written for SQL Server 2008.  Similarly, TSql90Parser class is available targeting the scripts written for SQL Server 2005.  A separate class TSqlAzureParser is also available in the same namespace targetting SQL Azure.

TSql100Parser has the .Parse method which takes TextReader as input and returns IList<ParseError> in the out parameter.  If the returning list contains any record, that mean the parsing has failed and the script has some errors.

Below is the code snippet for the parsing the SQL script: