Step 1:
Add Linq to Sql classes to your application,and make sure you selected the .NET 3.5 as target framework.
Step : 2
Add Server database to solution explorer.After adding Database diagram toggle to find the tables on selected database.
Step :3
Linq to SQL template name will becomes the parent class and retrieve the data from the table as below.
Snippet:
DataClasses2DataContext db2 = new DataClasses2DataContext();
var c = from sp in db.Peoples
select sp;
List
GridView1.DataSource = li.ToArray();
GridView1.DataBind();
Wednesday, November 11, 2009
LINQ to SQL Classes tutorial
Monday, November 02, 2009
Basic LINQ in C#
var c = new List left arrow bracket Customers right arrow bracket {
new Customers{Code=20,Name="Murugesan"},
new Customers{Code=21,Name="Pandian"},
new Customers{Code=22,Name="Senthilan"},
new Customers{Code=20,Name="Muthu"},
new Customers{Code=20,Name="Sachin"},
};
var result = from sp in c
select sp;
foreach (var Result in result)
{
Response.Write(Result.Name.ToString());
}
Class implementation
public class Customers
{
private int _code;
private string _name;
public int Code
{
get { return _code; }
set { _code = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
Sunday, November 01, 2009
Encrypting-Decrypting the password in C#
Encrypting the password in .NET become ease,DESCryptoServiceProvider enables the developer to encrypt the plain text in to encrypted form.
Code snippet as below :
public static byte[] Encrypt(string txtPassword, SymmetricAlgorithm key)
{
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, key.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cStream);
sw.WriteLine(txtPassword);
sw.Close();
cStream.Close();
byte[] buffer = mStream.ToArray();
mStream.Close();
return buffer;
}
Decrypting the encrypted bytes:
public static string Decrypt(byte[] CryptedBytes, SymmetricAlgorithm key)
{
MemoryStream mStream = new MemoryStream(CryptedBytes);
CryptoStream cStream = new CryptoStream(mStream, key.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cStream);
string PlainTxt = sr.ReadLine();
sr.Close();
cStream.Close();
mStream.Close();
return PlainTxt;
}
In Database,I used to save these bytes in Password column.While retrieving/Checking the Password,I used the Decrypt the function.So Password column will have encrypted text that easily human can't read or memorize.
For using this code you need to import System.Security.Cryptography class into your application.