c#数据绑定之linq使用示例
程序员文章站
2023-12-19 09:04:22
xaml:复制代码 代码如下:  ...
xaml:
复制代码 代码如下:
<grid>
<grid.columndefinitions>
<columndefinition width="2*"/>
<columndefinition width="8*"/>
</grid.columndefinitions>
<grid.rowdefinitions>
<rowdefinition />
<rowdefinition />
<rowdefinition />
</grid.rowdefinitions>
<listbox grid.column="1" margin="10" name="listbox1" />
<button content="linqtoentity" grid.column="0" margin="5" name="button1" click="button1_click" />
<listbox grid.column="1" grid.row="1" margin="10" name="listbox2" />
<button content="linqtosql" grid.column="0" grid.row="1" margin="5" name="button2" click="button2_click" />
<listbox grid.column="1" grid.row="2" margin="10" name="listbox3" />
<button content="lintoenclient" grid.column="0" grid.row="2" margin="5" name="button3" click="button3_click" />
</grid>
button1 linq to entity
复制代码 代码如下:
using (var context = new adventureworks2008entities())
{
//var people = context.people.where(c => c.lastname == "king").orderby(d => d.firstname).select(r => new { r.firstname,r.lastname});
//var people = context.people.where(c => c.lastname == "king").orderby(c =>c.firstname).select(c => new { c.firstname, c.lastname });
var people = from per in context.people
//join emp in context.employees on per.businessentityid equals emp.businessentityid
where per.lastname == "king"
orderby per.firstname
select new { per.firstname, per.lastname};
foreach (var person in people)
{
listbox1.items.add(string.format("{0} \t \t {1} ", person.firstname, person.lastname));
}
}
button2 linq to entitysql
复制代码 代码如下:
using (var context = new adventureworks2008entities())
{
var str = "select value p from adventureworks2008entities.people as p where p.lastname= @lastname order by p.firstname";
//var people = context.createquery<person>(str);
var people = new system.data.objects.objectquery<person>(str, context);
people.parameters.add(new system.data.objects.objectparameter("lastname", "king"));
foreach (var person in people)
{
listbox2.items.add(string.format("{0} \t \t{1}", person.firstname, person.lastname));
}
}
button3 linq to entityclient
复制代码 代码如下:
var firstname = "";
var lastname = "";
using (entityconnection conn = new entityconnection("name=adventureworks2008entities"))
{
string str = "select p.firstname, p.lastname from adventureworks2008entities.people as p where p.lastname='king' order by p.firstname";
conn.open();
entitycommand cmd = conn.createcommand();
cmd.commandtext =str;
using (entitydatareader rdr = cmd.executereader(system.data.commandbehavior.sequentialaccess))
{
while (rdr.read())
{
firstname = rdr.getstring(0);
lastname = rdr.getstring(1);
listbox3.items.add(string.format("{0}\t \t{1}", firstname, lastname));
}
}
conn.close();
}
}
推荐阅读
-
c#数据绑定之linq使用示例
-
c#数据绑定之向查询中添加参数(.Net连接外部数据库)
-
c# 使用Entity Framework操作Access数据库的示例
-
C#使用晚绑定来实现压缩Access数据库的方法
-
c# 使用Entity Framework操作Access数据库的示例
-
Vuejs学习笔记之使用指令v-model完成表单的数据双向绑定
-
C#使用checkedListBox1控件链接数据库的方法示例
-
html5本地存储之localstorage 、本地数据库、sessionStorage简单使用示例
-
Vuejs学习笔记之使用指令v-model完成表单的数据双向绑定
-
C#使用ODBC与OLEDB连接数据库的方法示例