LinQ介绍
1、语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言的扩展。它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据。
2、LinQ与ADO.NET区别在于后台封装访问类代码量上减少
LINQ to SQL 建立实体类
使用LINQ to SQL设计器建立实体类:
第一步:
第二步:找到LinQ to Sql类
第三步:点击图片从左到右第一个加号,连接数据库
第四步:需要那个数据表,就鼠标把这个数据表拖到空白区域
属性拓展:
字段扩展:建一个部分类(关键字:partial)
public partial class users属性拓展
{
public string SexStr { get { return Convert.ToBoolean(_sex) ? "images/man.png" : "images/woman.png"; } }
public string BirStr { get { return Convert.ToDateTime(this._birthday).ToString("yyyy年MM月dd日"); } }
public string NationName
{
get
{
string end = "<暂无>";
if (!String.IsNullOrEmpty(this._nation))
{
end = nation1.nationname;
}
return end;
}
}
public string ClassName
{
get
{
return class1.classname;
}
}
}
LinQ的基本增删改查
public partial class _Default : System.Web.UI.Page查询
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//绑定数据库数据
using (DataClassesDataContext con = new DataClassesDataContext())
{
Repeater1.DataSource = con.users.ToList();
Repeater1.DataBind();
}
}
Button1.Click += Button1_Click;
}
void Button1_Click(object sender, EventArgs e)
{
//查询你输入的内容,返回数据库该数据
using (DataClassesDataContext con = new DataClassesDataContext())
{
List<users> list = con.users.ToList();
if (TextBox1.Text != "")
{
List<users> list1 = con.users.Where(r => r.nickname.Contains(TextBox1.Text)).ToList();
list = list.Intersect(list1).ToList();
}
if (TextBox2.Text != "")
{
List<users> list1 = con.users.Where(r => Convert.ToDateTime(r.birthday).Year == Convert.ToInt32(TextBox2.Text)).ToList();
list = list.Intersect(list1).ToList();
}
if (TextBox3.Text != "")
{
List<users> list1 = con.users.Where(r => r.nation.Contains(TextBox3.Text)).ToList();
list = list.Intersect(list1).ToList();
}
Repeater1.DataSource = list;
Repeater1.DataBind();
}
}
}
public partial class Insert : System.Web.UI.Page添加
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (DataClassesDataContext con = new DataClassesDataContext())
{
DropDownList1.DataSource = con.nation.ToList();
DropDownList1.DataTextField = "NationName";
DropDownList1.DataValueField = "nationcode";
DropDownList1.DataBind();
DropDownList2.DataSource = con.@class.ToList();
DropDownList2.DataTextField = "ClassName";
DropDownList2.DataValueField = "classcode";
DropDownList2.DataBind();
}
}
Button1.Click += Button1_Click;
}
void Button1_Click(object sender, EventArgs e)
{
users u = new users();
u.usersname = TextBox1.Text;
u.password = TextBox2.Text;
u.nickname = TextBox3.Text;
if (Convert.ToBoolean(u.sex))
RadioButton1.Checked = true;
else
RadioButton2.Checked = true;
u.birthday = Convert.ToDateTime(TextBox4.Text);
u.nation = DropDownList1.SelectedValue;
u.@class = DropDownList2.SelectedValue;
try
{
using (DataClassesDataContext con = new DataClassesDataContext())
{
con.users.InsertOnSubmit(u);
con.SubmitChanges();
Response.Write("<script>");
Response.Write("alert('添加成功!');");
Response.Write("window.location.href='Default.aspx';");
Response.Write("</script>");
}
}
catch
{
Response.Write("<script>");
Response.Write("alert('添加失败!');");
Response.Write("</script>");
}
}
}
public partial class Delete : System.Web.UI.Page删除
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string un = Request["i"];
using (DataClassesDataContext con = new DataClassesDataContext())
{
users u = con.users.Where(r => r.usersname == un).FirstOrDefault();
if (u != null)
{
con.users.DeleteOnSubmit(u);
con.SubmitChanges();
}
}
Response.Redirect("Default.aspx");
}
}
}
public partial class Update : System.Web.UI.Page修改
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (DataClassesDataContext con = new DataClassesDataContext())
{
DropDownList1.DataSource = con.nation.ToList();
DropDownList1.DataTextField = "NationName";
DropDownList1.DataValueField = "nationcode";
DropDownList1.DataBind();
DropDownList2.DataSource = con.@class.ToList();
DropDownList2.DataTextField = "ClassName";
DropDownList2.DataValueField = "classcode";
DropDownList2.DataBind();
}
string un = Request["i"];
if (String.IsNullOrEmpty(un))
{
}
using (DataClassesDataContext con = new DataClassesDataContext())
{
users u = con.users.Where(r => r.usersname == un).FirstOrDefault();
if (u == null)
{
}
TextBox1.Text = u.usersname;
TextBox2.Text = u.password;
TextBox3.Text = u.nickname;
if (Convert.ToBoolean(u.sex))
RadioButton1.Checked = true;
else
RadioButton2.Checked = true;
TextBox4.Text = u.birthday.ToString();
DropDownList1.SelectedValue = u.nation;
DropDownList2.SelectedValue = u.@class;
}
}
Button1.Click += Button1_Click;
}
void Button1_Click(object sender, EventArgs e)
{
using (DataClassesDataContext con = new DataClassesDataContext())
{
users u = con.users.Where(r => r.usersname == TextBox1.Text.Trim()).FirstOrDefault();
u.password = TextBox2.Text;
u.nickname = TextBox3.Text;
if (RadioButton1.Checked)
u.sex = true;
else
u.sex = false;
u.birthday = Convert.ToDateTime(TextBox4.Text);
u.nation = DropDownList1.SelectedValue;
u.@class = DropDownList2.SelectedValue;
con.SubmitChanges();
Response.Redirect("Default.aspx");
}
}
}