ORM增删改查询例题
public partial class Form1 : Form
{
private MydbInfoDataContext context = new MydbInfoDataContext();
public Form1()
{
InitializeComponent();
}
//查询所有
private void butOK_Click(object sender, EventArgs e)
{
// var var = from p in context.Info select p;
var var = context.Info;
dataGridView1.DataSource = var;
}
//等值查询
private void butDengzhi_Click(object sender, EventArgs e)
{
//var var=from p in context.Info where p.Name=="胡军"select p;
var var = context.Info.Where(p => p.Name == "胡军");
dataGridView1.DataSource = var;
}
//不等值查询
private void butBudengzhi_Click(object sender, EventArgs e)
{
//var var = from p in context.Info where p.Code != "p001" select p;
var var = context.Info.Where(p => p.Code != "p002");
dataGridView1.DataSource = var;
}
//多条件查询
private void butDuotiaojian_Click(object sender, EventArgs e)
{
//var var = from p in context.Info where p.Sex == true && p.Nation == "n003" select p;
var var = context.Info.Where(p => p.Sex == true ).Where(p=>p.Nation=="n003");
dataGridView1.DataSource = var;
}
//数据添加
private void butADD_Click(object sender, EventArgs e)
{
Info data = new Info();
data.Code = txtcode.Text;
data.Name = txtname.Text;
data.Sex = Convert.ToBoolean (txtsex.Text);
data.Nation = txtnation.Text;
data.Birthday = Convert.ToDateTime(txtbirthday.Text);
context.Info.InsertOnSubmit(data);
context.SubmitChanges();
}
//数据在界面显示
private void Form1_Load(object sender, EventArgs e)
{
List<Info> list = context.Info.ToList();
dataGridView1.DataSource = list;
}
//删除数据
private void butdelete_Click(object sender, EventArgs e)
{
var var = context.Info.Where(p => p.Code == txtcode.Text);
if(var.Count()>0)
{
Info data = var.First();
context.Info.DeleteOnSubmit(data);
context.SubmitChanges();
}
}
//
private void txtcode_TextChanged(object sender, EventArgs e)
{
}
//修改数据
private void butUpdata_Click(object sender, EventArgs e)
{
var var = context.Info.Where (p => p.Code == txtcode.Text);
if(var.Count()>0)
{
Info data = var.First();
data.Code = txtcode.Text;
data.Name = txtname.Text;
data.Sex =Convert.ToBoolean( txtsex.Text);
data.Nation = txtnation.Text;
data.Birthday =Convert.ToDateTime (txtbirthday.Text);
}
context.SubmitChanges();
}
//模糊查询
private void butMohu_Click(object sender, EventArgs e)
{
//var var = from p in context.Info where p.Name.Contains("环") select p;
// var var = context.Info.Where(p => p.Name.StartsWith("环"));
// var var = context.Info.Where(p => p.Name.EndsWith("妹"));
var var =context.Info.Where(p=>p.Name.Substring(0,1)=="环");
dataGridView1.DataSource = var;
}
//集合操作
private void butJihecaozuo_Click(object sender, EventArgs e)
{
var var1 = context.Info.Where(p => p.Sex == true);
var var2 = context.Info.Where(p => p.Nation == "n003");
//相交部位
//var var = var1.Intersect(var2);
//var var = var1.Union(var2);
var var = var2.Except(var1);
dataGridView1.DataSource = var;
}
//统计函数 个数 总和 平均值 最大 最小
private void butTongji_Click(object sender, EventArgs e)
{
var var = context.Info.Where(p => p.Birthday.Value.Month > 5);
//显示月份在五月份的有2个
txtcode.Text = var.Count().ToString();
//显示当前日期月份减去查询人物生日月份的值
txtname.Text = var.Sum(p => DateTime.Now.Month - p.Birthday.Value.Month).ToString();
//当前日期月份减去查询日期月份的平均值
txtsex.Text = var.Average(p => DateTime.Now.Month - p.Birthday.Value.Month).ToString();
//当前日期月份减去查询日期月份的最大值
txtnation.Text = var.Max(p => DateTime.Now.Month - p.Birthday.Value.Month).ToString();
//当前日期月份减去查询日期月份的最小值
txtbirthday.Text = var.Min(p => DateTime.Now.Month - p.Birthday.Value.Month).ToString();
}
//转换操作
private void butZhuanHuan_Click(object sender, EventArgs e)
{
//转换成集合
var var = context.Info.Where(p => p.Birthday.Value.Month > 5);
List<Info> list = var.ToList();
//转换成数组
//var var = context.Info.Where(p => p.Birthday.Value.Month > 5);
//Info[] list = var.ToArray();
//转换成对象
//var var = context.Info.Where(p => p.Birthday.Value.Month > 5);
//Info data = var.First();
dataGridView1.DataSource = var;
}
//分页查询
private void butFenYe_Click(object sender, EventArgs e)
{
//跳过1条分成2页 显示2条
var var = context.Info.Skip(1 * 1).Take(2);
dataGridView1.DataSource = var;
}
}
|