Tuesday, May 28, 2013

How To: Replace a string in all files in a directory with Powershell

If we need to replace some string from all .txt files placed under C:\Myscript directory, below are the steps 1) Copy below script and paste in .ps1 file lets take Test.ps1 as example
param($R,$W,$Ext) ##Replaces all the occurrences in the Views folder## foreach ($f in Get-ChildItem -path "C:\MyScripts\" -Filter *.$Ext | sort-object) {(Get-Content $f.fullname) | Foreach-Object {$_ -replace "$R", "$W"} | Set-Content $f.fullname}
2) Go to Run 3) type Powershell 4) execute Test.ps1 "X" "Y" "txt" Test.ps1 will search all .txt files under C:\myscript folder and replace All X with Y. Thanks Pawan

Friday, November 26, 2010

Transaction Handling in C#

using System.Transactions;
Transaction on C# When handling only one Stored Procedure

using (TransactionScope transaction = new TransactionScope())
{
-- Write your code here it rolback if there is any issue in your query.

transaction.Complete();
}

Some time we need to update multiple table using multiple methods
at that time use

using (TransactionScope TranScope = new TransactionScope())
{
//Write your code here

TranScope.Complete();
}

Monday, October 11, 2010

SET vs SELECT when assigning variables

SET is the ANSI standard for variable assignment, SELECT is not.


SET can only assign one variable at a time, SELECT can make multiple assignments at once.

If assigning from a query, SET can only assign a scalar value. If the query returns multiple values/rows then SET will raise an error. SELECT will assign one of the values to the variable and hide the fact that multiple values were returned (so you'd likely never know why something was going wrong elsewhere - have fun troubleshooting that one)

When assigning from a query if there is no value returned then SET will assign NULL, where SELECT will not make the assignment at all (so the variable will not be changed from it's previous value)

As far as speed differences - there are no direct differences between SET and SELECT. However SELECT's ability to make multiple assignments in one shot does give it a slight speed advantage over SET.

Monday, October 4, 2010

SQL Server Join Hints

JOIN hints can be used in a query to specify the type of JOIN the Query Optimizer is to use for the execution plan. The JOIN options are:
  • Loop
  • Merge
  • Hash


The syntax for a JOIN hint is (using an INNER JOIN as an example):
FROM table_one INNER [LOOP | MERGE | JOIN] JOIN table_two
Here's an example:
FROM header_table INNER LOOP JOIN detail_table
As you can see, the JOIN hint is between the type of JOIN (in this example, INNER) and the JOIN keyword. Only one JOIN hint can be used per JOIN in a query. In addition, JOIN hints can only be used when the ANSI JOIN syntax is used, not the older Microsoft JOIN syntax.
The syntax above is not the only option to add a JOIN hint to a query. In addition, you can also use OPTION clause. Using the OPTION clause specifies that the hint be used throughout all of the query. While multiple hints can be added to the OPTION clause, each query hint can be used only once. In addition, only one OPTION clause can be used per query.
Here's an example of using the OPTION clause:

OPTION (INNER) or OPTION (MERGE) or OPTION (HASH)
The Query Optimizer always tries to identify the fastest way to JOIN tables. The fastest JOIN method is the Loop JOIN, followed by the Merge and the Hash JOIN. While the Query Optimizer always tries to perform a Loop JOIN if possible, it is not always possible, and one of the other two types may have to be used.


Tuesday, August 31, 2010

Top with percent in Sql Server 2005


In Sql Server if you want Specific percent of rows from table then you can use percent keyword 

Query to select all rows   
Select * from Employee

Query to select Top 2 Rows
Select top 2 * from Employee

Query to select 40% rows of total rows
Select top 40 percent * from Employee


Monday, August 16, 2010

Verbatim String in C#

I used to wonder, if I coud somehow represent "\" as "\" instead of the escape sequence  for black slash ("\\") in string. I have precisely come across this feature today in C-sharp and I would like to share it with you all.Especially in my last articles when I used the paths I did not feel like using "\\".
Other than the regular string literals, C# supports what is called as Verbatim string literals.Verbatim string literals begin with @" and end with the matching quote. They do not have escape sequences.
string path = @"C:\Program Files\My Program"; //verbatim literal
string path2 = "C:\\Program Files\\My Program"; //regular literal

string msg = @"Hello,
                        This is a multi-line string"; //verbatim literal

string msg2 = "Hello,\nThis is multi-line string"; //regular literal

Wednesday, August 11, 2010

this Keyword in C#

The this keyword refers to the current instance of the class. Static member functions do not have a this pointer. The this keyword can be used to access members from within constructors, instance methods, and instance accessors.

Example

In this example, this is used to qualify the Employee class members, name and alias, which are hidden by similar names. It is also used to pass an object to the method CalcTax, which belongs to another class.





































Output

Name: John M. Trainer
Alias: jtrainer
Taxes: $240.00

Monday, August 2, 2010

Now its time for desi Browser

If you are using Internet Explorer or Firefox or Chrome or Opera or Safari for browsing give it a Desi change with Epic – A New Browser from India. The Epic browser is developed by Bangalore based Hidden Reflex and is based on Mozilla and is available for free. What is very important is the news of Epic could give Moksha or liberation to millions of Indian netizens who are stuck with Internet Explorer 6.

Download - Epic browser here at the official website.

Monday, July 26, 2010

Use explicit casting instead of DataBinder.Eval

The DataBinder.Eval method uses .NET reflection to evaluate the arguments that are passed in and to return the results. Consider limiting the use of DataBinder.Eval during data binding operations in order to improve ASP.NET page performance.
Consider the following ItemTemplate element within a Repeater control using DataBinder.Eval:




Using explicit casting offers better performance by avoiding the cost of .NET reflection. Cast the Container.DataItem as a DataRowView:


Monday, July 19, 2010

SQL DATE Time Convert

During Sql Queries most of the time we need to manipulate the format of Date below I am trying to cover most of the format of date time in sql queries.

-- SQL Server T-SQL date and datetime formats - sql date / datetime format
-- Date time formats - mssql datetime - sql server date formats - sql dates format
-- MSSQL getdate returns current system date and time in standard internal format
-- SQL datetime formats with century (YYYY or CCYY format)- sql time format
SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM)
-- Oct 2 2010 11:01AM
SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy - 10/02/2010
SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd - 2010.10.02
SELECT convert(varchar, getdate(), 103) -- dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) -- dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) -- dd mon yyyy
SELECT convert(varchar, getdate(), 107) -- mon dd, yyyy
SELECT convert(varchar, getdate(), 108) -- hh:mm:ss
SELECT convert(varchar, getdate(), 109) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)
-- Oct 2 2010 11:02:44:013AM
SELECT convert(varchar, getdate(), 110) -- mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) -- yyyy/mm/dd
-- yyyymmdd - ISO date format - international standard - works with any language setting
SELECT convert(varchar, getdate(), 112) -- yyyymmdd
SELECT convert(varchar, getdate(), 113) -- dd mon yyyy hh:mm:ss:mmm
-- 02 Oct 2010 11:02:07:577
SELECT convert(varchar, getdate(), 114) -- hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) -- yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) -- yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) -- yyyy-mm-ddThh:mm:ss.mmm
-- 2010-10-02T10:52:47.513
-- Without century (YY) date / datetime conversion - there are exceptions!
SELECT convert(varchar, getdate(), 0) -- mon dd yyyy hh:mmAM (or PM)
SELECT convert(varchar, getdate(), 1) -- mm/dd/yy
SELECT convert(varchar, getdate(), 2) -- yy.mm.dd
SELECT convert(varchar, getdate(), 3) -- dd/mm/yy
SELECT convert(varchar, getdate(), 4) -- dd.mm.yy
SELECT convert(varchar, getdate(), 5) -- dd-mm-yy
SELECT convert(varchar, getdate(), 6) -- dd mon yy
SELECT convert(varchar, getdate(), 7) -- mon dd, yy
SELECT convert(varchar, getdate(), 8) -- hh:mm:ss
SELECT convert(varchar, getdate(), 9) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)
SELECT convert(varchar, getdate(), 10) -- mm-dd-yy
SELECT convert(varchar, getdate(), 11) -- yy/mm/dd
SELECT convert(varchar, getdate(), 12) -- yymmdd
SELECT convert(varchar, getdate(), 13) -- dd mon yyyy hh:mm:ss:mmm
SELECT convert(varchar, getdate(), 14) -- hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 20) -- yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 21) -- yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 22) -- mm/dd/yy hh:mm:ss AM (or PM)
SELECT convert(varchar, getdate(), 23) -- yyyy-mm-dd
SELECT convert(varchar, getdate(), 24) -- hh:mm:ss
SELECT convert(varchar, getdate(), 25) -- yyyy-mm-dd hh:mm:ss.mmm
-- SQL create different date styles with t-sql string functions
SELECT replace(convert(varchar, getdate(), 111), '/', ' ') -- yyyy mm dd
SELECT convert(varchar(7), getdate(), 126) -- yyyy-mm
SELECT right(convert(varchar, getdate(), 106), 8) -- mon yyyy
SELECT substring(convert(varchar, getdate(), 120),6, 11) -- mm-dd hh:mm