C# Method: Encrypting a string using MD5 algorithm

20. July 2009 00:40

C# Code Snippets 

Encrypting a string using the MD5 algorithm

This C# method will encrypt any string using MD5 algorithm. It generates the same hash as the PHP MD5() function

using System;using System.Collections.Generic;
using System.Linq;using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
namespace EncryptString
{
    class Program
    {
        public static string EncodePassword(string originalPassword)
        {
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);
            return Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();
         }
         static void Main(string[] args)
         {
            Console.WriteLine("Enter a string to Encrypt");
            string strEnc = Console.ReadLine();
            Console.WriteLine(EncodePassword(strEnc));
            Console.ReadLine();
         }
    }
}
Currently rated 3.0 by 2 people

Insert binary files to SQL server using BULK operation

11. July 2009 18:15

T-SQL 

Performing BULK operations with SQL can be useful when you are trying to insert any type of file. Don't mess up with the complex code, just use a simple SQL query to insert any file type in SQL table. 

To perform this operation first you need to have a column with datatype as varbinary(max). See below example:

Create a table with an ID and Data as columns with INT and VARBINARY(MAX) as their Datatypes respectively.

Create table tblData
(
FileId int,
FileData varbinary(max)
)

Perform INSERT to Save a file. Here just change the location of the file you want to insert

Insert into tblData
(FileId, FileData)
SELECT 1, BulkColumn
FROM OPENROWSET(BULK N'D:\Software\siw.exe', SINGLE_BLOB) AS Document

Display table data after INSERT

Select * from tblData
Currently rated 3.0 by 2 people

GUIDs in C# / SQL

26. June 2009 15:37

C# SQL Server Code Snippets 

GUID pronounced as goo'id - Globally unique identifier is a 128-bit integer that can be used to uniquely identify something. If you want to identify the large collection of data GUID is the best approach in my view. Also you can use the autoincremental integer to identify records, this is the basic approach programmer's use.

GUID method can be found under the System namespace and it also provides some overloads. Here is how we can use the GUID overloads:

System.Guid.NewGuid().ToString();    //c93ded50-7cce-4f3a-b8aa-5a1a34d80b14
System.Guid.NewGuid().ToString("N"); //636bdfd2bbc541229fbc6b869995ce6f
System.Guid.NewGuid().ToString("D"); //45e602b6-0904-47ac-a764-a5522f56f40f
System.Guid.NewGuid().ToString("B"); //{68f55508-f72c-4a38-b96b-f493c3c5adb9}
System.Guid.NewGuid().ToString("P"); //(1c2e52a7-e384-445e-b80a-e8704c6ecbd6)

To create GUID in SQL Server use the simple select statement:

SELECT NEWID()

 No Rating

Backup/Restore SQL database using C#

21. June 2009 15:59

C# SQL Server Utils 

Microsoft’s .NET framework provides the flexibility to Backup and Restore SQL server database. A few lines to populate the names of all the server instances, create backups and then restore the backups. The application what you see here is a part of an application, so when you create backups and restore it will use the same path hardcoded in the code itself. You can change the way you like it.


Namespace you need to add:  

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;

I am not going to write on and on and just make it going boring, so it's better just download the code and check the comments, they are one liners, but clears everything. 

Download Full Application Here: DBBackup.zip (290.99 kb)

Currently rated 3.6 by 19 people

Microsoft Surface

21. June 2009 15:33

Microsoft 

I know it is too late to post this, but still it's new for many people out there. Cool

 

[flv:Microsoft_Surface.flv]

 No Rating