Labels

Showing posts with label Software Engineering. Show all posts
Showing posts with label Software Engineering. Show all posts

Friday, August 19, 2016

How to pass parameter to an Exe

When building desktop applications, you often need to pass data between different programs. Fortunately, there is a common method for passing parameters to an executable (.exe) whether you are using WPF or Windows Forms. By leveraging command-line arguments, you can trigger specific behaviors or load specific data into your application upon startup. Below, I’ll show you the universal way to implement this.

1. Add reference System.Diagnostics your class.                                                             


2. Then need start process.                                                                                    

                                                                                          ProcessStartInfo processStrInfo = new ProcessStartInfo();
              processStrInfo.FileName =   /// Exe path;
              processStrInfo.Arguments =  /// Parameter to pass as string;
              processStrInfo.UseShellExecute = false;
              Process.Start(processStrInfo);                                                                                                              

3. parameter can send as string.


           string tempCommandArgs ="TestPara";                 


             ProcessStartInfo processStrInfo = new ProcessStartInfo();
             processStrInfo.FileName = "test.exe";  /// Exe path;
             processStrInfo.Arguments = tempCommandArgs  /// Parameter to pass as string;
             processStrInfo.UseShellExecute = false;
             Process.Start(processStrInfo);   

4 .When we passing multiple parameter, we can use space between parameters .


          tempCommandArgs  =COMPANYNAME +" "+USERID

5. If parameter value have spaces.

When a parameter contains spaces—for example, a company name like 'Test Company'—you must wrap the string in double quotes. This ensures the operating system treats the entire phrase as a single argument. In your code, you can achieve this by adding a double-quote character at the start and end of the parameter string."
   
    In this time we can use below method (add stroke to start of parameter and end of parameter)

    tempCommandArgs  ="\"" + COMPANYNAME + "\"" + " "
                                                  +"\"" + USERlOGINNAME. + "\"";    

Note:-
This is way to pass parameter to Exe as argument , But running exe from another running application (exe) is not good practice. It will consume some more memory

Now take a look at how to consume passed argument form exe.

Note:-
This example I have done with WPF application

1. We can override OnStartup in App.xaml.cs

 protected override void OnStartup(StartupEventArgs e)
  {          
            string[] args = Environment.GetCommandLineArgs();
            args = e.Args;
         
            if (args.Count() > 0)
            {            
                COMPANYNAME = args[0];        
                USERNAME = args[1];
            }

 }

Thursday, July 7, 2016

Common Table Expression (CTE) in Sql Server

Common Table Expressions, or CTEs, can be thought of as temporary views that are created in memory for a single execution scope. They allow you to simplify complex joins and subqueries by defining a named result set that exists only until the next query runs. Whether you are using Recursive or Non-Recursive CTEs, understanding their limitations—such as being unindexable and requiring immediate execution—is key to writing efficient SQL code.

  • We can take CTE as tempory view (can be thought as tempory result set) that create in memory
  • Its scope valid until next query run
  • CTE need to use just after it declaration  
  • There are two type Recursive and Nonrecursive CTE
  • Can define multiple CTE
  • CTE are unindexable ( it cau use exist index in referenced object)
  • CTE can't have constrain
  • ORDER BY clause can't be used in the CTE_Query_Definition , execpt when use TOP keyword


Simple Example.

ex 1
WITH CTE_CUSTOMER_EX2
(         [CustomerID]
          ,[CompanyName]
  ,[ContactName]
  ,[ContactTitle]
  ,[Address]
  ,[City]
)
AS
(
SELECT [CustomerID]
  ,[CompanyName]
  ,[ContactName]
  ,[ContactTitle]
  ,[Address]
  ,[City]

  FROM .[dbo].[Customers]
)


SELECT * FROM CTE_CUSTOMER_EX2


ex 2

;WITH CTE_CUSTOMER_EX1
AS
(
SELECT [CustomerID]
 ,[CompanyName]
 ,[ContactName]
 FROM .[dbo].[Customers]

)
SELECT * FROM CTE_CUSTOMER_EX1


Note

If there are query before CTE define , need to use Semicolon teminator before declare CTE


Multiple CTE


  With CTE_M1 as 

(
-select statement

     , CTE_M2 as 
(
-select statemet

)


you can find some more detail and examples using this Link and Link2