Beginning LINQ to SQL Using Visual Studio 2008 - Developing a simple LINQ to SQL application (without LINQ designer): source in C#
(Page 3 of 6 )
To code in C#, follow the steps in the previous section with the following code (UI Design would be the same).
The following is the "emp" class written in C#:
using System.Data.Linq.Mapping;
namespace SampleCS
{
[Table(Name="emp")]
public class Emp
{
[Column(Name = "empno", IsPrimaryKey = true)]
public Int32 empno { get; set; }
[Column(Name = "ename")]
public string ename { get; set; }
[Column(Name = "sal")]
public System.Nullable<double> sal { get; set; }
[Column(Name = "deptno")]
public System.Nullable<Int32> deptno { get; set; }
}
}
The ASPX code would be the same as given in the previous section. The following is the code for code-behind (default.aspx.cs):
using System.Data.Linq;
namespace SampleCS
{
public partial class _Default : System.Web.UI.Page
{
protected void btnSelectStar_Click(object sender, EventArgs e)
{
DataContext db = new DataContext(new System.Data.SqlClient.SqlConnection("Data Source=.sql2k5;initial catalog=sample;user id=sa; password=eXpress2005"));
Table<Emp> tblEmps = db.GetTable<Emp>();
this.GridView1.DataSource = tblEmps;
this.GridView1.DataBind();
}
}
}
Next: CRUD operations using the LINQ to SQL application (without LINQ designer): UI Design >>
More .NET Articles
More By Jagadish Chaterjee