ENVIRONMENT:
- Create DataClasses1.dbml.
- Create a database Test with table test which has Id (integer, PK) and Name (varchar(10)).
- Create a stored procedure GetDatas
- Using server explorer connect to database Test and add the table "test" and Stored proceure "getDatas" in the DataClasses1.dbml
* Once the dbml file is created system automatically creates a class "DataClasses1DataContext"
EXAMPLES
- Retrieve data from Table test
using (DataClasses1DataContext dc = new DataClasses1DataContext())
IEnumerablet = from b in dc.tests select b; //where b.name.StartsWith("Ra")
foreach (test t1 in t)
{
Console.WriteLine(t1.name);
}
} - retrieve data from Stored Procedure
using (DataClasses1DataContext dc = new
{
var t = from b in dc.getDatas() select b;
foreach (var t1 in t)
{
Console.WriteLine(t1.name);
}
} - Insert in to Table test
using (DataClasses1DataContext dc = new DataClasses1DataContext())
{
test d = new test();
d.name = 'n';
dc.duplicates.InsertOnSubmit(d);
dc.SubmitChanges();
} - Updation
using (DataClasses1DataContext dc = new DataClasses1DataContext())
{
IEnumerablet = from b in dc.tests select b; //where b.name.StartsWith("Ra")
//Update Table
test t2 = t.Single((a) => a.ID == 1);
t2.name = "updated";
dc.SubmitChanges();
}
achieve this add attribute to any one of the column for that table in DataClasses1DataContext class as follows
[Column(Storage = "_id", DbType = "Int", IsPrimaryKey = true)]
public System.Nullable
{
get
{
return this._id;
}
set
{
if ((this._id != value))
{
this._id = value;
}
}
}