Wednesday

T-SQL (Microsoft SQL Server): How to find table by column name:

Run this to find table with column name:

SELECT o.name AS TableName, c.name AS ColumName
FROM syscolumns c JOIN sysobjects o
ON c.id = o.id
WHERE c.name like '%account%'
ORDER BY o.name, c.name

more...

serializable abstract class, can be used for inheritance , C# .net

can be used for serialize object to xml


abstract public class AbstractXMLObject
{
public string XML
{
get
{
XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter,this);
return stringWriter.ToString();
}
set
{
XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
StringReader stringReader = new StringReader(value);
this = xmlSerializer.Deserialize(stringReader);
}
}
}


more...

Tuesday

.net Threading:Passing parameters to the thread for ParameterizedThreadStart in C# (csharp / dot.net)


0.Create MultiThreadArgument class and add all parameters for thread starting 
public class MultiThreadArgument {
public MerchantBase merchant;
public TransactionCollection transactionCollection;
public string email;
}

1.Thread starter in main thread class (thread class):
private void ThreadStarter(MerchantBase m, TransactionCollection c,string email){
if (c.Count > 0) {
Thread th = new Thread(new ParameterizedThreadStart(ThreadWorker));
// create class parameters :
MultiThreadArgument ma = new MultiThreadArgument();
ma.merchant = m;
ma.transactionCollection = c;
ma.email = email;
th.IsBackground = true;
th.Start(ma);
}
}

2.Thread worker in main class:
private void ThreadWorker(Object o) {
MultiThreadArgument ma = (MultiThreadArgument)o;
DateTime st = DateTime.Now;
string mid = ma.merchant.ID.ToString();
Logger.prn("*** Processing started for merchant #", mid , "transactions - ", ma.transactionCollection.Count.ToString());
ma.merchant.ProcessRequest(ma.transactionCollection, this);
.....
}

more...

Friday

appent new element to string[] array

to add:

string[] newarr = new string[oldarr.Length + 1];
oldarr.CopyTo(newarr, 0);
newarr[newarr.Length-1] = "new element";
return newarr;

more...

casting Array into string[]


return Array.ConvertAll<object, string>(ls, delegate(object obj) { return (string) obj; });

more...

Thursday

how to get file name and file extentions [ C# / CSarp]

by System.IO.Path class in dot.net framework:

string
file_extention;
string file_name;
file_name=Path.GetFileName(this.mffFileName);
file_extention = Path.GetExtension(this.mffFileName);

more...

TimeSpan parse method (part of the info from msdn)

Here is format of input string :

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]


Items in square brackets ([ and ]) are optional; one selection from the list of alternatives enclosed in braces ({ and }) and separated by vertical bars (|) is required; colons and periods (: and .) are literal characters and required; other items are as follows.

Item

Description

ws

optional white space

"-"

optional minus sign indicating a negative TimeSpan

d

days, ranging from 0 to 10675199

hh

hours, ranging from 0 to 23

mm

minutes, ranging from 0 to 59

ss

optional seconds, ranging from 0 to 59

ff

optional fractional seconds, consisting of 1 to 7 decimal digits

The components of s must collectively specify a time interval greater than or equal to MinValue and less than or equal to MaxValue.



days , hours , minutes , but what if it's required to have timespan in a weeks, month , years ?
more...

Wednesday

operator overloading example c#

Samples of string and bool operator overloading in C-Sharp, this code inside of class Result:
 1   public static implicit operator bool(Result p){
2 return p.Status == enResultStatus.OK;
3 }
4
5 private const string of = "\nResult:\n\tstatus:{0}\n\tdesc:{1}\n\tMsg:{2}\n\tException:{3}\n\tval:{4}\n\tSuggested:{5}";
6 public static implicit operator string(Result p) {
7 return String.Format(of, p.Status , p.Description, p.UserFriendlyErrorMsg,p.ExeptionHappend,p.Value,p.SuggestedValue);
8 }

Usage:
15  Result res;
16 if (res) {
17 Debug.WriteLine("Dump of result object :"+res )
18 }


more...

Monday

sample of sorting algoritm for collection of Invoice objects by date-time

Collection is type of System.Collections.Generic.List

// sorting class 
public class InvoiceSorterbyDueDate : System.Collections.Generic.IComparer<BaseObject>
{
#region IComparer<Invoice> Members
public int Compare(BaseObject x, BaseObject y){
return ((Invoice)x).DueDate.CompareTo(((Invoice)y).DueDate);
}
#endregion
}


//call
IComparer<BaseObject> ins = new InvoiceSorterbyDueDate();
p.InvoiceCollectionToCheck.Sort(ins);

//


more...

python time or datetime format and function

1 import time
2 timestring = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
more...

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...