Wednesday, February 29, 2012

mail Asynchronously delegate callback


 
Public Sub sendMail(ByVal subject As String, ByVal message As String)
        Try
            Dim destinations As New ArrayList
             destinations.Add(123@hotmail.com)
            Dim errDesc As String
            'mail is another function to send email
            mail("mail server", "username", "password", "from", subject, message, destinations, Nothing, Nothing, errDesc)
        Catch
        End Try
    End Sub

Public Class SendMailAsyn
    Public Sub sendMailAsyn(ByVal subject As String, ByVal message As String)
        Dim dlgt As New SendMailDelegate(AddressOf sendMail)
        Dim cb As New AsyncCallback(AddressOf SendEmailResponse)
        dlgt.BeginInvoke(subject, message, cb, dlgt)
    End Sub

Public Delegate Sub SendMailDelegate(ByVal subject As String, ByVal message As String)

    'callback method
    Public Sub SendEmailResponse(ByVal ar As IAsyncResult)
        Dim dlgt As SendMailDelegate = CType(ar.AsyncState, SendMailDelegate)
        dlgt = ar.AsyncState
        dlgt.EndInvoke(ar)
    End Sub
End Class

'catch exception and send email
Dim mail As New SendMailAsyn
 mail.sendMailAsyn("Exception", ex.Message)

Sunday, February 12, 2012

while
int n = 1;
      while (n < 6)
      {
         Console.WriteLine("Current value of n is {0}", n);
         n++;
      }
do
      int x;
      int y = 0;
      do
      {
         x = y++;
         Console.WriteLine(x);
      }
      while(y < 5);
for
for (int i = 1; i <= 5; i++)
         Console.WriteLine(i);

for each
public static void Main()
   {
      int odd = 0, even = 0;
      int[] arr = new int [] {0,1,2,5,7,8,11};
      foreach (int i in arr)
      {
         if (i%2 == 0) 
            even++;     
         else
            odd++;        
      }
      Console.WriteLine("Found {0} Odd Numbers, and {1} Even Numbers.",
                        odd, even) ;
   }

Transaction

1.ADO.net
using(Sqlconnection con = new SqlConnection(constr))
{
SqlTransaction transaction;
try{
con.Open();
transactiom = con.BeginTransacton();
SqlCommand cmd = new SqlCommand();
com.Connection = con;
cmd.Transaction = transaction;
cmd.CommandText="Sql1";
cmd.ExecuteNonQuery();
transaction.Commit();
Console.Write("successeful");
}
catch {
transaction.Rollback();
Console.WriteLine("failed");
}
}

2.直接写入到sql 中 使用 BEGIN TRANS, COMMIT TRANS, ROLLBACK TRANS 实现:
例如
BEGIN TRANS
DECLARE @orderDetailsError int, @productError int
DELETE FROM /"Order Details/" WHERE ProductID=42
SELECT @orderDetailsError = @@ERROR
DELETE FROM Products WHERE ProductID=42
SELECT @productError = @@ERROR
IF @orderDetailsError = 0 AND @productError = 0
COMMIT TRANS
ELSE
ROLLBACK TRANS
这种方法比较简单,具体可以查阅相关sql server 帮助

Sunday, February 5, 2012

try catch & DB Connection

SqlConnection conn = null;
SqlCommand cmd = null;

try
{
    conn = new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString)
    cmd = new SqlCommand(reportDataSource, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year;
    cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start;
    cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = end;
        cmd.Open();

    DataSet dset = new DataSet();
    new SqlDataAdapter(cmd).Fill(dset);
    this.gridDataSource.DataSource = dset.Tables[0];
}
catch(Exception ex)
{
    Logger.Log(ex);
    throw;
}
finally
{
    if(conn != null)
        conn.Dispose();

        if(cmd != null)
        cmd.Dispose();
}

*****************
 Try        
Catch ex As Exception            
'Exception handling here        
Finally 'Clean Up
If Conn.State = ConnectionState.Open Then Conn.Close()            
If Conn IsNot Nothing Then Conn.Dispose()            
If cmd IsNot Nothing Then cmd.Dispose()
  'AndAlso only tests 2nd condition if 1st is 'True'            
If myReader IsNot Nothing AndAlso Not (myReader.IsClosed) Then myReader.Close()        
End Try

************
Public Function ExecuteNonQuery(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As Integer
            Dim connection As SqlConnection = Nothing
            Dim transaction As SqlTransaction = Nothing
            Dim command As SqlCommand = Nothing
            Dim res As Integer = -1
            Try
                connection = New SqlConnection(_connectionString)
                command = New SqlCommand(cmd, connection)
                command.CommandType = cmdType
                Me.AssignParameters(command, parameters)
                connection.Open()
                transaction = connection.BeginTransaction()
                command.Transaction = transaction
                res = command.ExecuteNonQuery()
                transaction.Commit()
            Catch ex As Exception
                If Not (transaction Is Nothing) Then
                    transaction.Rollback()
                End If
                Throw New SqlDatabaseException(ex.Message, ex.InnerException)
            Finally
                If Not (connection Is Nothing) AndAlso (connection.State = ConnectionState.Open) Then connection.Close()
                If Not (command Is Nothing) Then command.Dispose()
                If Not (transaction Is Nothing) Then transaction.Dispose()
            End Try
            Return res
        End Function


************
 Finally
                'Lily on Feb 2012
                If Not (reader.IsClosed) Then
                    reader.Close()
                End If
                If reader IsNot Nothing Then
                    reader = Nothing
                End If
                If Not (ppResult Is Nothing) Then
                    ppResult = Nothing
                End If
                If Not (pp Is Nothing) Then
                    pp.Dispose()
                    pp = Nothing
                End If
                If cmd IsNot Nothing Then
                    cmd.Dispose()
                End If
                cmd = Nothing
                If conn.State = ConnectionState.Open Then
                    conn.Close()
                End If
                If conn IsNot Nothing Then
                    conn.Dispose()
                End If
                conn = Nothing
                If flagAR = False Then
                    Threading.Thread.CurrentThread.Sleep(timeReTry)
                End If
            End Try