Wednesday, January 21, 2009

C#: upload Audio Files in Sql Server

Code to upload Audio/Image Files in Sql Server


protected void Button1_Click(object sender, EventArgs e)
{
Int32 intAudioSize = 0;
string strAudioType = null;
Stream AudioStream = null;

// Gets the Size of the Audio
intAudioSize = FileUpload1.PostedFile.ContentLength;

// Gets the Audio Type
strAudioType = FileUpload1.PostedFile.ContentType;

// Reads the Audio
AudioStream = FileUpload1.PostedFile.InputStream;

byte[] AudioContent = new byte[intAudioSize + 1];
int intStatus = 0;
intStatus = AudioStream.Read(AudioContent, 0, intAudioSize);

// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Insert into AudioTable (Audio,AudioType) Values (@Audio,@AudioType)", myConnection);

// Mark the Command as a SPROC/Query
myCommand.CommandType = CommandType.Text;

// Add Parameters to SPROC
SqlParameter prmAudio = new SqlParameter("@Audio", SqlDbType.Image);
prmAudio.Value = AudioContent;
myCommand.Parameters.Add(prmAudio);

SqlParameter prmAudioType = new SqlParameter("@AudioType", SqlDbType.VarChar, 255);
prmAudioType.Value = strAudioType;
myCommand.Parameters.Add(prmAudioType);

try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("New audio file uploaded successfully!");
}
catch (SqlException SQLexc)
{
Response.Write("Insert Failed. Error Details are: " + SQLexc.ToString());
}
}

C#: Bucket Sort

A simple and effective algorithm for sorting integers in C#


public static void BucketSort(int[] integers)
{
//Verify input
if (integers == null integers.Length <= 1)
return;

//Find the maximum and minimum values in the array
int maxValue = integers[0]; //start with first element
int minValue = integers[0];

//Note: start from index 1
for (int i = 1; i < integers.Length; i++)
{
if (integers[i] > maxValue)
maxValue = integers[i];

if (integers[i] < minValue)
minValue = integers[i];
}

//Create a temporary "bucket" to store the values in order
//each value will be stored in its corresponding index
//scooting everything over to the left as much as possible (minValue)
//e.g. 34 => index at 34 - minValue
LinkedList[] bucket = new LinkedList[maxValue - minValue + 1];

//Move items to bucket
for (int i = 0; i < integers.Length; i++)
{
if (bucket[integers[i] - minValue] == null)
bucket[integers[i] - minValue] = new LinkedList();

bucket[integers[i] - minValue].AddLast(integers[i]);
}

//Move items in the bucket back to the original array in order
int k = 0; //index for original array
for (int i = 0; i < bucket.Length; i++)
{
if (bucket[i] != null)
{
LinkedListNode node = bucket[i].First; //start add head of linked list

while (node != null)
{
integers[k] = node.Value; //get value of current linked node
node = node.Next; //move to next linked node
k++;
}
}
}
}

.NET: Extract numbers from string

VB.NET


Shared Function ExtractNumbers(ByVal expr As String) As String
Return String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(expr, "[^\d]"))
End Function

C#


static string ExtractNumbers(string expr)
{
return string.Join(null,System.Text.RegularExpressions.Regex.Split(expr, "[^\\d]"));
}



Call the function as follows

VB.NET



Response.Write (ExtractNumbers("12EFR77"))


C#



Response.Write (ExtractNumbers("12EFR77"));


Output of this Code will be: 1277

SQL: Read error log

In SQL Server Database, There is plenty of external storedprocedure is available.

To Read Error Log, we can use xp_readerrorlog, which is a external SP.

see the following code, it let us know, how to use this SP

This extended stored procedure will return a errorlog file.



EXEC master..xp_readerrorlog

C#: Create PDF Document

Following Steps are involved to create a PDF document using C#

1) Create an instance of document object
Document myDoc = new Document(PageSize.A4.Rotate());

2) Create a writer that listens to this doucment and writes the document to desired Stream
PdfWriter.GetInstance(myDoc, new FileStream("Mathews.pdf", FileMode.Create));

3) Open the document
myDoc.Open();

4) Add Content to it
myDoc.add( new Paragraph ( "I am adding some content"));

5) Close the document
myDoc.close();

To use this sample source code for creating a PDF document, you must add a reference to the assembly iTextSharp.sourceforge.net. This can be downloaded from http://www.lowagie.com/iText/. DOwnload the assembly from the website http://www.lowagie.com/iText/ and add a reference to that from your project.


public static void Main()
{
Console.WriteLine("iText Demo");
// create a document-object Document myDoc = new Document(PageSize.A4.Rotate());
try
{
// create a writer
PdfWriter.GetInstance(myDoc, new FileStream("Mathews.pdf", FileMode.Create));

// Open
myDoc.Open();
// add contents
myDoc.Add(new Paragraph("Hello all"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// close the documnet
myDoc.Close();
}