LinqToSQL简单的增,册,改,查
//查詢所有的信息并显示新的字段
static void ObjectQuery()
{
var db =new NorthwindDataContext();
var result = from b in db.Company from c in db.BaseData2s select new { c.EngName, b.ChiName };
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.Read();
}
//添加数据
static void AddData()
{
var db = new NorthwindDataContext();
int count = db.Company.Where(c => c.ChiName == "123").Count();
Console.WriteLine("state:{0}",count);
var newCompary = new Company() { ChiName="123",ID=888 };
db.Company.InsertOnSubmit(newCompary);
db.SubmitChanges();
int count2 = db.Company.Where(c => c.ChiName == "123").Count();
Console.WriteLine("state:{0}", count2);
Console.Read();
}
//更新数据
static void ModifyData()
{
var db = new NorthwindDataContext();
int count = db.Company.Where(c => c.ChiName == "456").Count();
Console.WriteLine(count);
var company = db.Company.FirstOrDefault();
company.ChiName = "456";
db.SubmitChanges();
int count2 = db.Company.Where(c => c.ChiName == "456").Count();
Console.WriteLine(count2);
Console.Read();
}
//数据排序
static void OrderQuery()
{
var db = new NorthwindDataContext();
var result=from b in db.BaseData2s orderby b.ID descending select new {b.ChiName,b.ID};
foreach (var item in result)
{
Console.WriteLine(item);
}
}
//数据分组
static void GroupData()
{
var db = new NorthwindDataContext();
var result = from b in db.BaseData2s group b by b.ChiName into g orderby g.Count() select new {num=g.Count(),chiname=g.Key };
foreach (var item in result)
{
Console.WriteLine(item);
}
}
static void ObjectQuery()
{
var db =new NorthwindDataContext();
var result = from b in db.Company from c in db.BaseData2s select new { c.EngName, b.ChiName };
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.Read();
}
//添加数据
static void AddData()
{
var db = new NorthwindDataContext();
int count = db.Company.Where(c => c.ChiName == "123").Count();
Console.WriteLine("state:{0}",count);
var newCompary = new Company() { ChiName="123",ID=888 };
db.Company.InsertOnSubmit(newCompary);
db.SubmitChanges();
int count2 = db.Company.Where(c => c.ChiName == "123").Count();
Console.WriteLine("state:{0}", count2);
Console.Read();
}
//更新数据
static void ModifyData()
{
var db = new NorthwindDataContext();
int count = db.Company.Where(c => c.ChiName == "456").Count();
Console.WriteLine(count);
var company = db.Company.FirstOrDefault();
company.ChiName = "456";
db.SubmitChanges();
int count2 = db.Company.Where(c => c.ChiName == "456").Count();
Console.WriteLine(count2);
Console.Read();
}
//数据排序
static void OrderQuery()
{
var db = new NorthwindDataContext();
var result=from b in db.BaseData2s orderby b.ID descending select new {b.ChiName,b.ID};
foreach (var item in result)
{
Console.WriteLine(item);
}
}
//数据分组
static void GroupData()
{
var db = new NorthwindDataContext();
var result = from b in db.BaseData2s group b by b.ChiName into g orderby g.Count() select new {num=g.Count(),chiname=g.Key };
foreach (var item in result)
{
Console.WriteLine(item);
}
}