普通的增删改查list View与LinQ:集成化语言增删改查与扩展属性
普通增加:
SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=789234178"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "insert into fruit values(@Ids,@Name,@Price,@Source,@Numbers,@Image)"; cmd.Parameters.Add("@Ids", textBox1.Text); cmd.Parameters.Add("@Name", textBox2.Text); cmd.Parameters.Add("@Price", textBox3.Text); cmd.Parameters.Add("@Source", textBox4.Text); cmd.Parameters.Add("@Numbers", textBox5.Text); cmd.Parameters.Add("@Image", textBox6.Text); conn.Open(); cmd.ExecuteNonQuery(); conn.Close();
List View控件:
要想从List View中显示列表,首先要从属性中最下面的View中
改为Details是专门用来显示列表的
属性中columns集合是用来显示列,Items是用来显示每一行,在text里面填写数据
在List View中查询:
SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=789234178"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select*from fruit"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { ListViewItem lvt=new ListViewItem(); lvt.Text=dr["ids"].ToString(); lvt.SubItems.Add(dr["name"].ToString()); lvt.SubItems.Add(dr["price"].ToString()); lvt.SubItems.Add(dr["source"].ToString()); lvt.SubItems.Add(dr["numbers"].ToString()); lvt.SubItems.Add(dr["image"].ToString()); listView1.Items.Add(lvt); } conn.Close();
在List View中删除:
在List view中要想删除一行但只能选择一个单元格的情况下从属性里面找到FullrowSelect选中True就可以选择一行删除了。
LinQ:集成化语言增删改查 O--M--R Object:对象 Mapping:映射 Relation:关系
用法: 一、简单的查询
造一个上下文对象:
HRDataContext _context = new HRDataContext();
扩展方法,查所有
dataGridView1.DataSource = _context.Info;
根据条件查,括号里要写Lambda表达式
List<Info> list = _context.Info.Where(p=>p.列名=="p001" || p.Name=="张三").ToList();
二、添加数据:
1.造一个上下文对象 HRDataContext _context = new HRDataContext();
2.造对象 Info data = new Info();
data.Code = "p005";
data.Name = "张三";
data.Sex = true;
data.Nation = "n001";
data.Birthday = DateTime.Now;
3.添加
_context.Info.InsertOnSubmit(data);
4.提交
_context.SubmitChanges();
三、修改对象
1.造一个上下文对象
方法一:
HRDataContext _context = new HRDataContext();
2.找出要修改的对象
List<Info> list = _context.Info.Where(p=>p.Code=="p005").ToList();
3.判断有没有找出数据
if (list.Count > 0)
{ Info data = list.First();
data.Name = "李四";
提交修改
_context.SubmitChanges();
方法二:
var query = _context.Info.Where(p => p.Code == "p001");
Info data = query.First();
data.Name = "aa";
_context.SubmitChanges();
四、删除对象
方法一:
1.造一个上下文对象
HRDataContext _context = new HRDataContext();
2.找出要修改的对象
List<Info> list = _context.Info.Where(p => p.Code == "p001").ToList();
3.判断有没有找出数据
if (list.Count > 0)
{ Info data = list.First();
删除
_context.Family.DeleteAllOnSubmit(data.Family);
_context.Work.DeleteAllOnSubmit(data.Work);
_context.Info.DeleteOnSubmit(data);
}
4.提交
_context.SubmitChanges();
方法二:
//var query = _context.Info.Where(p=>p.Code=="p001");
//Info data = query.First();
var query1 = _context.Family.Where(p => p.InfoCode == data.Code);
_context.Family.DeleteAllOnSubmit(query1);
var query2 = _context.Work.Where(p => p.InfoCode == data.Code);
_context.Work.DeleteAllOnSubmit(query2);
_context.Info.DeleteOnSubmit(data);
_context.SubmitChanges();
扩展属性:
partial class Info
{
public string SexName
{
get {
return this.Sex.Value ? "男" : "女";
}
}
public string NationName
{ get {
//TestDataContext context = new TestDataContext();
//Nation data = context.Nation.Where(p=>p.Code == this.Nation).First();
//return data.Name;
return this.Nation1.Name;
}
}
} }