tag:blogger.com,1999:blog-16442781976927271352009-06-29T20:40:40.111+02:00Dotnet4all Code SnippetsXanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-1644278197692727135.post-24511285600144505972008-12-08T18:59:00.005+01:002008-12-30T23:03:43.839+01:00Remove content between HEAD tags using Regex and c#To remove all content between HEAD-tags from a webpage using Regex and C# use the following code snippet:
private string RemoveContentBetweenHeadTags(string in_HTML)
{
string lv_HTML = in_HTML;
lv_HTML = Regex.Replace(lv_HTML, "<head.*?</head>", ""
, RegexOptions.Singleline RegexOptions.IgnoreCase);
XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-72870391152890892992008-12-08T18:44:00.007+01:002008-12-08T19:07:17.798+01:00Strip HTML tags, HEAD content and SCRIPT tags from content using Regex and C#This code snippet shows how to remove HTML tags, content within HEAD-elements and content within SCRIPT-elements using C# and Regex.
Fill the string 'in_HTML' with a HTML-formatted webpage. The funtion will return a string with the content from that webpage, excluding the Header info and Javascripts.
private string RemoveHTML(string in_HTML)
{
string lv_HTML = XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-84709941763248240582008-04-06T12:34:00.003+02:002009-06-17T07:10:51.974+02:009 Tips for creating indexes in SQL ServerTo improve performance in SQL Server the first step is to create indexes on the appropriate table-fields. Below a list of 9 tips when creating indexes.
1) Create indexes on the highly selective colums that are used in the WHERE-clause
2) Create indexes on all columns that are used in the WHERE clause in case OR is used
3) Create at least a clustered index on every table. Generally use the columnXanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-4220812744721105072008-04-06T12:11:00.004+02:002009-06-17T07:09:51.590+02:00Performance Tip 1: Avoid non-sargable WHERE-clauses.Avoid non-sargable WHERE-clauses. If possible rewrite them to sargable ones.
In case a WHERE-clause is sargable SQL Server can take advantage of an index to speed up the execution of the query. In case it is non-sargable SQL Server can' t.
Sargable operators:
= (best sargable operator)
>
<
>=
<=
EXIST
IS
IN
BETWEEN
LIKE 'abc%' (least best sargable operator)
Non-sargable operators
IS NULL
<>
!XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-5445754366728631372008-04-06T12:04:00.005+02:002009-06-17T07:08:54.897+02:0023 Tips to improve the performance of your SQL queriesI've created a list with 23 tips to improve your SQL Server queries. Quite often these tips can also be used for other database-engines like MS Access, Oracle or MySQL.
1) Avoid non-sargable WHERE-clauses. If possible rewrite them to sargable ones
2) In the WHERE-clause use the least likely true AND expression first
3) Avoid using OR in the WHERE-clause if not all colums have an index
4) Avoid XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-27546010557174951422008-04-05T21:02:00.015+02:002009-06-17T07:07:23.368+02:00A cheat sheet for SQL Server developersI have created a cheat sheet (factsheet) for SQL Server (6.5, 7.0, 2000 and 2005) developers that fits on one A4 (printed on both sides) so that you can laminate it and keep it on your desk. You have to print it with a color-printer to be able to use all information!
An overview of the contents of this factsheet:- A checklist for writing fast queries
- Sargability of queries
- A checklist for XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com9tag:blogger.com,1999:blog-1644278197692727135.post-48096582661382406992007-05-09T20:10:00.000+02:002007-05-09T20:45:23.215+02:00How to replace certain word with a hyperlink using C#This code shows, using C#, how to build a function that replaces certain word (-combinations)with a hyperlink and returns a HTML formatted string.
public static string InsertHyperLink(string in_Text, string in_TextToHyperLink, string in_HyperLink)
{
int lv_Pointer = 0;
while (lv_Pointer > -1 && lv_Pointer < in_Text.Length)
{
lv_Pointer = in_Text.ToLower().IndexOf(XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-22267689651376874232007-05-09T19:53:00.000+02:002007-05-09T20:06:02.797+02:00How to Highlight a specific word in HTML content (C#)This code snippet shows how to hightlight (or apply a certain style to) a specific word (or combination of words) from a striong in a browser.
public static string HighLight(string in_Text, string in_TextToHighLight, string lv_Style)
{
int lv_Pointer = 0;
while (lv_Pointer > -1)
{
lv_Pointer = in_Text.ToLower().IndexOf(in_TextToHighLight.ToLower(),lv_Pointer);
if (lv_Pointer >= 0)XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-67494449539240333462007-04-22T17:12:00.000+02:002007-04-22T17:21:10.136+02:00how to extract SRC from IMG elements in HTML codeThis piece of code shows how to extract the SRC URL from the IMG element in HTML code, using a regular expression (RegEx). Every match is put into an Array.
public static ArrayList ExtractAllImagesFromHTMLbyURL(string lv_HTML)
{
ArrayList lv_Images = new ArrayList();
try
{
//Find SRC URL from IMG tag
Regex lv_FindAllImages = new Regex(@"]*src\s*=\s*[\""\']?(?
[^""'>\s]*)[\""\'XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-77842419563160071832007-04-16T14:39:00.000+02:002007-04-16T14:40:51.055+02:00How to extract URL and Anchor from HTML contentThis C# snippet shows how to extract URL/Anchor combinations from HTML content and store the URLs in an ArraList.
to achieve this two regular expressions are used.
- One regular expression extracts all URL/Anchor combination from HTML content
- One regular expression check if a string is a qualified URL. (for example not a relative one)
Snippet:
//Define ArrayList in which all URLs will be XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-25412402807327877302007-04-16T12:45:00.000+02:002007-04-22T13:44:02.115+02:00Grab the content of a (GZIP) webpage using C#This code snippet demonstrates how to grab the content from a webpage and put it in a string variable. This snippet can be used in several applications like a webcrawler or spider.
Since Bandwith (most of the times) is an issue I also have added code that enables and handles GZIP/DEFLATE compressed content. Compressed content can save up to 80% of the needed bandwith, since most of the content XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com1tag:blogger.com,1999:blog-1644278197692727135.post-53048891411948549602007-04-16T12:16:00.000+02:002007-04-16T13:32:28.091+02:00How to extract the host name from an URL (C#)This snippet demonstrates how to extract the host/domain name from a valid URL using regular expressions.
using System.Text.RegularExpressions;
...
public static string ExtractDomainFromURL(string in_URL)
{
string regexPattern = @"^(?(?[^:/\?#]+):)?(?"
+ @"//(?[^/\?#]*))?(?[^\?#]*)"
+ @"(?\?(?[^#]*))?"
+ XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com2tag:blogger.com,1999:blog-1644278197692727135.post-14709675992852326582007-04-16T12:10:00.000+02:002007-04-16T12:21:36.283+02:00How to Send an email using SMTP (C#)Sending Email is easy. The only conditions is that you have to have access to an SMTP mailing service.
using System.Web.Mail;
...
public static void SendF4Mail(string in_Body, string in_Subject, _
string in_From, string in_To, string in_Bcc)
{
MailMessage lv_Mail = new MailMessage();
string lv_MySMTPServer = "smtp.xxxxx.xxxx"; //Your SMTP server
XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0tag:blogger.com,1999:blog-1644278197692727135.post-87772749271461195992007-04-16T11:56:00.000+02:002007-04-16T13:36:29.883+02:00How to remove HTML-tags from web content (C#)Using webcontent in applications can be very annoying since webcontent usually contains lots of HTML elements. With one simple action, using regular expressions, all of these HTML elements can be removed from the content. What's left is a clean string, without HTML formatting.
Snippet:
using System.Text.RegularExpressions;
...
public static string RemoveHTML(string in_HTML)
{
XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com4tag:blogger.com,1999:blog-1644278197692727135.post-8105867495393254202007-04-16T11:40:00.000+02:002007-04-16T12:09:25.431+02:00How to convert DateTime to SQL valid stringUsing DateTime-fields in an SQL string is not difficult. Therefore the DateTime format must be 'YYYY-MM-DD HH:mm'.
For Example:
"Select * from MyTable Where MyDateTimeField > '2007-01-01 12:00'"
For converting a .NET DateTime object to a valid SQL-compliant string using C# the following code snippet can be used:
public static string ConverteerDatum(DateTime in_Datum)
{
return = XanderZehttp://www.blogger.com/profile/11484871454554231807noreply@blogger.com0