.Net Framework, Asp.net, Ado.net, .Net Remoting, .Net Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML

Ado.netAsp.Net 2.0SQLC-SharpXMLFrameworkIISXMLWebServiceArchitecture
UMLProject ManagementSDLCMethodologiesDesign PatterenOOPWCF.Net RemotingWWF
MVPMVCSilver LightN-HibernateAjaxJ-QuerySEO-MarketingSite MapPhp

LINQ to SQL Example, Linq to Dataset

LINQ is a new component in .net 3.5 Framework

Here i will explain how we can interact with database using LINQ stucture

Assuming that you have already ceated a web application and a data accesss class libary, The web application has the refference of class library.
Now follow the steps below.
1. Right click on class library => Add New Item.
2. Select "LINQ to Sql class".
3. Now at Server Explorer add a database.
4. Drag the table on dbml file you have created.
5. Now you can see a datacontext file is created, inherited from System.Data.Linq.DataContext

Example shows how the code will look like in .dbml designer file.
Model code sample
#pragma warning disable 1591
1://------------------------------------------------------------------------------
2:// <auto-generated>
3:// This code was generated by a tool.
4:// Runtime Version:2.0.50727.3074
5://
6:// Changes to this file may cause incorrect behavior and will be lost if
7:// the code is regenerated.
8:// </auto-generated>
9://------------------------------------------------------------------------------
10:
11:namespace ETG.Base.Applications.Consultancy.DTO
12:{
13: using System.Data.Linq;
14: using System.Data.Linq.Mapping;
15: using System.Data;
16: using System.Collections.Generic;
17: using System.Reflection;
18: using System.Linq;
19: using System.Linq.Expressions;
20: using System.ComponentModel;
21: using System;
22:
23:
24: [System.Data.Linq.Mapping.DatabaseAttribute(Name="ETGConsultancy")]
25: public partial class DTOConsultancyDataContext : System.Data.Linq.DataContext
26: {
27:
28: private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
29:
30: #region Extensibility Method Definitions
31: partial void OnCreated();
32: partial void InserttbTechnology(tbTechnology instance);
33: partial void UpdatetbTechnology(tbTechnology instance);
34: partial void DeletetbTechnology(tbTechnology instance);
35: #endregion
36:
37: public DTOConsultancyDataContext() :
38: base(global::ETG.Base.Applications.Consultancy.Properties.Settings. Default.ETGConsultancyConnectionString, mappingSource)
39: {
40: OnCreated();
41: }
42:
43: public DTOConsultancyDataContext(string connection) :
44: base(connection, mappingSource)
45: {
46: OnCreated();
47: }
48:
49: public DTOConsultancyDataContext(System.Data.IDbConnection connection) :
50: base(connection, mappingSource)
51: {
52: OnCreated();
53: }
54:
55: public DTOConsultancyDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
56: base(connection, mappingSource)
57: {
58: OnCreated();
59: }
60:
61: public DTOConsultancyDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
62: base(connection, mappingSource)
63: {
64: OnCreated();
65: }
66:
67: public System.Data.Linq.Table<tbTechnology> tbTechnologies
68: {
69: get
70: {
71: return this.GetTable<tbTechnology>();
72: }
73: }
74: }
75:
76: [Table(Name="dbo.tbTechnology")]
77: public partial class tbTechnology : INotifyPropertyChanging, INotifyPropertyChanged
78: {
79:
80: private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
81:
82: private int _TechnologyId;
83:
84: private string _Name;
85:
86: private string _Description;
87:
88: private System.Nullable<System.DateTime> _CreatedOn;
89:
90: private string _Author;
91:
92: #region Extensibility Method Definitions
93: partial void OnLoaded();
94: partial void OnValidate(System.Data.Linq.ChangeAction action);
95: partial void OnCreated();
96: partial void OnTechnologyIdChanging(int value);
97: partial void OnTechnologyIdChanged();
98: partial void OnNameChanging(string value);
99: partial void OnNameChanged();
100: partial void OnDescriptionChanging(string value);
101: partial void OnDescriptionChanged();
102: partial void OnCreatedOnChanging(System.Nullable<System.DateTime> value);
103: partial void OnCreatedOnChanged();
104: partial void OnAuthorChanging(string value);
105: partial void OnAuthorChanged();
106: #endregion
107:
108: public tbTechnology()
109: {
110: OnCreated();
111: }
112:
113: [Column(Storage="_TechnologyId", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
114: public int TechnologyId
115: {
116: get
117: {
118: return this._TechnologyId;
119: }
120: set
121: {
122: if ((this._TechnologyId != value))
123: {
124: this.OnTechnologyIdChanging(value);
125: this.SendPropertyChanging();
126: this._TechnologyId = value;
127: this.SendPropertyChanged("TechnologyId");
128: this.OnTechnologyIdChanged();
129: }
130: }
131: }
132:
133: [Column(Storage="_Name", DbType="NChar(50)")]
134: public string Name
135: {
136: get
137: {
138: return this._Name;
139: }
140: set
141: {
142: if ((this._Name != value))
143: {
144: this.OnNameChanging(value);
145: this.SendPropertyChanging();
146: this._Name = value;
147: this.SendPropertyChanged("Name");
148: this.OnNameChanged();
149: }
150: }
151: }
152:
153: [Column(Storage="_Description", DbType="NChar(500)")]
154: public string Description
155: {
156: get
157: {
158: return this._Description;
159: }
160: set
161: {
162: if ((this._Description != value))
163: {
164: this.OnDescriptionChanging(value);
165: this.SendPropertyChanging();
166: this._Description = value;
167: this.SendPropertyChanged("Description");
168: this.OnDescriptionChanged();
169: }
170: }
171: }
172:
173: [Column(Storage="_CreatedOn", DbType="DateTime")]
174: public System.Nullable<System.DateTime> CreatedOn
175: {
176: get
177: {
178: return this._CreatedOn;
179: }
180: set
181: {
182: if ((this._CreatedOn != value))
183: {
184: this.OnCreatedOnChanging(value);
185: this.SendPropertyChanging();
186: this._CreatedOn = value;
187: this.SendPropertyChanged("CreatedOn");
188: this.OnCreatedOnChanged();
189: }
190: }
191: }
192:
193: [Column(Storage="_Author", DbType="NChar(20)")]
194: public string Author
195: {
196: get
197: {
198: return this._Author;
199: }
200: set
201: {
202: if ((this._Author != value))
203: {
204: this.OnAuthorChanging(value);
205: this.SendPropertyChanging();
206: this._Author = value;
207: this.SendPropertyChanged("Author");
208: this.OnAuthorChanged();
209: }
210: }
211: }
212:
213: public event PropertyChangingEventHandler PropertyChanging;
214:
215: public event PropertyChangedEventHandler PropertyChanged;
216:
217: protected virtual void SendPropertyChanging()
218: {
219: if ((this.PropertyChanging != null))
220: {
221: this.PropertyChanging(this, emptyChangingEventArgs);
222: }
223: }
224:
225: protected virtual void SendPropertyChanged(String propertyName)
226: {
227: if ((this.PropertyChanged != null))
228: {
229: this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
230: }
231: }
232: }
233:}
234:#pragma warning restore 1591
235:

Now see how we can add data using linq structure, the following button has a OnClick="btnAdd_Click" event handler.
Button click code behind
public void btnAdd_Click(object sender, EventArgs e)
1: {
2: using (DTOConsultancyDataContext _consultancy = new DTOConsultancyDataContext())
3: {
4: tbTechnology row=new tbTechnology();
5: row.Author = "Arindam Chakraborty";
6: row.Description = "This is example of LINQ to SQL";
7: row.CreatedOn = DateTime.Now.Date;
8: row.Name = "LINQ";
9:
10:
11: _consultancy.tbTechnologies.InsertOnSubmit(row);
12: _consultancy.SubmitChanges();
13:
14: }
15: }
16:
InsertOnSubmit(entity) method perform the data insertion on _consultancy.SubmitChanges();
Leave your comment.
Name
Email
Comments
Tell Us2060+5 =

Reduce your Dialy IT Cost, Consult with us
Stay healthy in recession

Advertisement
Sponsored by

Privacy Policy ©2009 ETG Consultancy, All Rights Reserved Terms & Conditions
Asp.net, Ado.net, .Net Remoting, .Net Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML