Monday, October 20, 2008

Clear database log files

USE database_name
GO
DBCC SHRINKFILE(database_name_log, 1)
BACKUP LOG database_name WITH TRUNCATE_ONLY
DBCC SHRINKFILE(database_name_log, 1)

Delete Records and Reset Identity

Truncate table table_name (we use it when the table has no references)

(or)

delete from table_name
DBCC CHECKIDENT (table_name, RESEED, 0)

(we use it when the table is mapped with other table)

Delete all Stored Procedures in a Database

use [Database_name]

declare @procName varchar(500)

declare cur cursor

for Select [name] from sys.procedures where [type] = 'P' and is_ms_shipped = 0 and [name] not like 'sp[_]%diagram%'

open cur
fetch next from cur into @procName
while @@fetch_status = 0
begin

exec('drop procedure ' + @procName)
fetch next from cur into @procName
end
close cur
deallocate cur

Friday, October 17, 2008

SQL Server 2000 vs. SQL Server 2005

1) The Data Transformation Service (DTS) system (sql server 2000) has been replaces by the SQL System Integration Service (SSIS) in sql server 2005.
2)The GUI management system is much more integrated now with the functionality of both Enterprise Manager and Query Analyser in the same application (SQL Server Management Studio).
3) Exception handling concept is new in sql server 2005. We can use Try Catch block to catch the exception. This feature is limited in sql server 2000 like @@error to show the error, no much flexibility..

Friday, October 3, 2008

Thumbnail Creation

Hi,

The following code is used to create a thumbnail image from the original image using C# code.

string file = "D:\\dcraw\\exif.jpg"; //Request.QueryString["file"];

// create an image object, using the filename we just retrieved
System.Drawing.Image image = System.Drawing.Image.FromFile(file); //Server.MapPath(file));

// create the actual thumbnail image
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

// make a memory stream to work with the image bytes
MemoryStream imageStream = new MemoryStream();

// put the image into the memory stream
thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

// make byte array the same size as the image
byte[] imageContent = new Byte[imageStream.Length];

// rewind the memory stream
imageStream.Position = 0;

// load the byte array with the image
imageStream.Read(imageContent, 0, (int)imageStream.Length);

// return byte array to caller with image type
Response.ContentType = "image/jpeg";
Response.BinaryWrite(imageContent);
Hi,

Here is my sample website to check the particular mail id is present or not in any domain.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.CSharp;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void BtnCheck_Click(object sender, EventArgs e)
{
int result;
result = ChatMailServer(TextBox1.Text.Trim());

if (result == 1)
{
Label1.Text = "Valid E-mail Address";
}
if (result == 2)
{
Label1.Text = "Not a Valid E-mail Address";
}
if (result == 3)
{
Label1.Text = "Error Occured but Domain was GOOD";
}
if (result == 4)
{
Label1.Text = "Domain was invalid";
}
}
public int ChatMailServer(string sEmail)
{
NetworkStream oStream;
string sFrom;
string sTo;
string sResponse;
string Remote_Addr;
string mserver;
string[] sText;
int returnvalue = 0;
sTo = "<" + sEmail + ">";
sText = sEmail.Split('@');
mserver = NSLookup(sText[1]);
if (mserver == "")
{
returnvalue = 4;
return returnvalue;

}
Remote_Addr = "sify.com";
sFrom = "";
TcpClient oConnection = new TcpClient();

try
{
oConnection.SendTimeout = 3000;
oConnection.Connect(mserver, 25);
oStream = oConnection.GetStream();
sResponse = GetData(oStream);
sResponse = TalkToServer(oStream, "HELO " + Remote_Addr + "\n");
sResponse = TalkToServer(oStream, "MAIL FROM: " + sFrom + "\n");
if (ValidResponse(sResponse))
{
sResponse = TalkToServer(oStream, "RCPT TO: " + sTo + "\n");
if (ValidResponse(sResponse))
{
returnvalue = 1;
}
else
{
returnvalue = 2;
}
}
TalkToServer(oStream, "QUIT" + "\n");
oConnection.Close();
oStream = null;
}
catch
{
returnvalue = 3;
}

return returnvalue;
}
private string GetData(NetworkStream oStream)
{
byte[] bResponse = new byte[1024];
string sResponse = "";
int lenStream = 0;

lenStream = oStream.Read(bResponse, 0, 1024); // Doubt

if (lenStream > 0)
{
sResponse = Encoding.ASCII.GetString(bResponse, 0, 1024);
}
return sResponse;

}
private string SendData(NetworkStream oStream, string sToSend)
{
string sResponse;
byte[] bArray = Encoding.ASCII.GetBytes(sToSend.ToCharArray());

oStream.Write(bArray, 0, bArray.Length);
sResponse = GetData(oStream);

return sResponse;
}
private bool ValidResponse(string sResult)
{
bool bResult = false;
int iFirst;

if (sResult.Length > 1)
{
iFirst = Convert.ToInt32(sResult.Substring(0, 1));
if (iFirst < 3)
{
bResult = true;
}

}

return bResult;
}
private string TalkToServer(NetworkStream oStream, string sToSend)
{
string sresponse;

sresponse = SendData(oStream, sToSend);

return sresponse;
}
private string NSLookup(string sDomain)
{
ProcessStartInfo info = new ProcessStartInfo();
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.FileName = "nslookup";
info.Arguments = "-type=MX " + sDomain.ToUpper().Trim();

Process ns;
ns = Process.Start(info);
StreamReader sout;
sout = ns.StandardOutput;
Regex reg = new Regex("mail exchanger = (?[^\\\\\\s]+)");
string mailserver = "";
string response = "";
do
{
response = sout.ReadLine();
Match amatch = reg.Match(response);
Debug.WriteLine(response);
if (amatch.Success)
{
mailserver = amatch.Groups["server"].Value;
}
} while (sout.Peek() > -1);

return mailserver;

}
}

Capture the screen

Hi all,

Here is my small code to capture the screen using C# windows application.

public class ScreenCapture
{
public Image CaptureScreen()
{
return CaptureWindow( User32.GetDesktopWindow() );
}

public Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle,ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest,hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle,hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}

public void CaptureWindowToFile(IntPtr handle, string filename, System.Drawing.Imaging.ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename,format);
}

public void CaptureScreenToFile(string filename, System.Drawing.Imaging.ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename,format);
}

private class GDI32
{

public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
int nWidth,int nHeight,IntPtr hObjectSource,
int nXSrc,int nYSrc,int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
}

private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
}
}

private void button1_Click(object sender, EventArgs e)
{
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureScreen();
pictureBox1.Image = img;
sc.CaptureWindowToFile(this.Handle, "D:\\temp2.gif", System.Drawing.Imaging.ImageFormat.Gif);
}