How to insert,delete,retrieve records from Database using LINQ?


To insert,delete,retrieve records from Database using LINQ

1. Rightclick from your project , choose Add item -> select LINQ to SQL Classes template
2. Below Default eInsuranceDataContext.dbml will be display change the name or simply enter
3. Now it will create eInsuranceDataContext.dbml file under your App_Code folder
4. Double click the eInsuranceDataContext.dbml file rightside you can see link button as server explorer.
5. Click Server Explorer , server explorer window will open , choose which table you want to use and drag into the design (for ex.. now i am going to use Newsmast table)
6. Once Dragged into design mode, eInsuranceDataContext.designer.cs ,can see the auto generation code for your table


Insert values into DB


public bool SaveNews(string strTitle, string strDsc, string strRefNo, byte[] Doc, string strExt)
{


bool flag = false;
objeDC = new eInsuranceDataContext();
objNewsMast = new NewsMast();

try
{
if (strExt.StartsWith("image"))
{
var Query = from objNewsMasts in objeDC.NewsMasts
where objNewsMasts.LatestInd == true
select objNewsMasts;
foreach (var Res in Query)
{
Res.LatestInd = false;
}
objNewsMast.Dsc = strDsc;
objNewsMast.Title = strTitle;
objNewsMast.RefNo = strRefNo;
objNewsMast.NewsTypeKey = "NW";
objNewsMast.Doc = Doc;
objNewsMast.Ext = strExt;
objNewsMast.TimeStamp = DateTime.Now;
objNewsMast.LatestInd = true;
}
else
{
objNewsMast.Dsc = strDsc;
objNewsMast.Title = strTitle;
objNewsMast.RefNo = strRefNo;
objNewsMast.NewsTypeKey = "NW";
objNewsMast.Doc = Doc;
objNewsMast.Ext = strExt;
objNewsMast.TimeStamp = DateTime.Now;
objNewsMast.LatestInd = false;
}
objeDC.NewsMasts.InsertOnSubmit(objNewsMast);
objeDC.SubmitChanges();
flag = true;
}
catch
{
throw;
}
finally
{
objeDC = null;
objNewsMast = null;
}
return flag;
}


Retrieve the information based on condition and bind into datalist


public void FilterNews(DataList dlList, int tmpValue,out string Extension)
{
objeDC = new eInsuranceDataContext();
Extension = null;
try
{

var objResult = from objTemp in
( from objNewsMast in objeDC.NewsMasts
where objNewsMast.NewsTypeKey == "NW" && objNewsMast.NewsMastKey == tmpValue
select new
{
Date = objNewsMast.TimeStamp,
Title = objNewsMast.Title,
Desc = objNewsMast.Dsc,
Extension =objNewsMast.Ext
}).ToList()
select new
{

Date = objTemp.Date.ToString("dd/MM/yyyy"),
Title = objTemp.Title,
Desc = objTemp.Desc,
Extension = objTemp.Extension
};

dlList.DataSource = objResult;
dlList.DataBind();
foreach (var Res in objResult)
{
Extension = Res.Extension;
}

}
catch
{
throw;
}
}



Delete

public bool DeleteNews(int strSysID)
{
objeDC = new eInsuranceDataContext();

try
{
objNewsMast = objeDC.NewsMasts.SingleOrDefault(Rec => Rec.NewsMastKey == strSysID);
if (objNewsMast != null)
{
objeDC.NewsMasts.DeleteOnSubmit(objNewsMast);
objeDC.SubmitChanges();
return true;
}
else
{
return false;
}
}
catch
{
throw;
}
finally
{
objeDC = null;
objNewsMast = null;
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: