不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
SQL Server数据库ADO.NET实体数据模型使用示例
编辑:dnawo 日期:2012-08-29
一、添加SQL Server数据库ADO.NET实体数据模型
①.添加新项并选择"ADO.NET实体数据模型":

②.选择从数据库生成模型:

③.设置数据库连接:

④.选择要在模型中包含的数据库对象,点击完成生成实体数据模型:

二、将存储过程转为函数
打开模型浏览器,右键点击需要使用的存储过程,在菜单中选择添加函数导入:


三、ADO.NET 实体数据模型使用示例
①.增加记录:
SQL Server Profiler跟踪结果:
引用内容
②.修改记录:
SQL Server Profiler跟踪结果:
引用内容
说明:不是全部字段值提交到服务器,仅提交修改部分!
③.删除记录:
SQL Server Profiler跟踪结果:
引用内容
④.查询记录:
SQL Server Profiler跟踪结果:
引用内容
⑤.调用存储过程:
SQL Server Profiler跟踪结果:
引用内容
①.添加新项并选择"ADO.NET实体数据模型":

②.选择从数据库生成模型:

③.设置数据库连接:

④.选择要在模型中包含的数据库对象,点击完成生成实体数据模型:

二、将存储过程转为函数
打开模型浏览器,右键点击需要使用的存储过程,在菜单中选择添加函数导入:


三、ADO.NET 实体数据模型使用示例
①.增加记录:
复制内容到剪贴板
程序代码

using (NorthwindEntities northwind = new NorthwindEntities())
{
Categories entity = new Categories();
entity.CategoryName = "fruit";
entity.Description = "-";
northwind.Categories.AddObject(entity);
northwind.SaveChanges();
}
{
Categories entity = new Categories();
entity.CategoryName = "fruit";
entity.Description = "-";
northwind.Categories.AddObject(entity);
northwind.SaveChanges();
}
SQL Server Profiler跟踪结果:

exec sp_executesql N'insert [dbo].[Categories]([CategoryName], [Description], [Picture])
values (@0, @1, null)
select [CategoryID]
from [dbo].[Categories]
where @@ROWCOUNT > 0 and [CategoryID] = scope_identity()',N'@0 nvarchar(15),@1 nvarchar(max) ',@0=N'fruit',@1=N'-'
values (@0, @1, null)
select [CategoryID]
from [dbo].[Categories]
where @@ROWCOUNT > 0 and [CategoryID] = scope_identity()',N'@0 nvarchar(15),@1 nvarchar(max) ',@0=N'fruit',@1=N'-'
②.修改记录:
复制内容到剪贴板
程序代码

using (NorthwindEntities northwind = new NorthwindEntities())
{
Categories entity = northwind.Categories.First(e => e.CategoryName == "fruit");
entity.Description = "fruit description.";
northwind.SaveChanges();
}
{
Categories entity = northwind.Categories.First(e => e.CategoryName == "fruit");
entity.Description = "fruit description.";
northwind.SaveChanges();
}
SQL Server Profiler跟踪结果:

exec sp_executesql N'update [dbo].[Categories]
set [Description] = @0
where ([CategoryID] = @1)
',N'@0 nvarchar(max) ,@1 int',@0=N'fruit description.',@1=24
set [Description] = @0
where ([CategoryID] = @1)
',N'@0 nvarchar(max) ,@1 int',@0=N'fruit description.',@1=24
说明:不是全部字段值提交到服务器,仅提交修改部分!
③.删除记录:
复制内容到剪贴板
程序代码

using (NorthwindEntities northwind = new NorthwindEntities())
{
Categories entity = northwind.Categories.First(e => e.CategoryName == "fruit");
northwind.Categories.DeleteObject(entity);
northwind.SaveChanges();
}
{
Categories entity = northwind.Categories.First(e => e.CategoryName == "fruit");
northwind.Categories.DeleteObject(entity);
northwind.SaveChanges();
}
SQL Server Profiler跟踪结果:

exec sp_executesql N'delete [dbo].[Categories]
where ([CategoryID] = @0)',N'@0 int',@0=24
where ([CategoryID] = @0)',N'@0 int',@0=24
④.查询记录:
复制内容到剪贴板
程序代码

using (NorthwindEntities northwind = new NorthwindEntities())
{
var categories = northwind.Categories;
foreach (var item in categories)
Console.WriteLine("{0},{1}", item.CategoryName, item.Description);
}
{
var categories = northwind.Categories;
foreach (var item in categories)
Console.WriteLine("{0},{1}", item.CategoryName, item.Description);
}
SQL Server Profiler跟踪结果:

Select
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[CategoryName] AS [CategoryName],
[Extent1].[Description] AS [Description],
[Extent1].[Picture] AS [Picture]
FROM [dbo].[Categories] AS [Extent1]
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[CategoryName] AS [CategoryName],
[Extent1].[Description] AS [Description],
[Extent1].[Picture] AS [Picture]
FROM [dbo].[Categories] AS [Extent1]
⑤.调用存储过程:
复制内容到剪贴板
程序代码

using (NorthwindEntities northwind = new NorthwindEntities())
{
var query = northwind.CustOrdersDetail(10284);
foreach (var item in query)
Console.WriteLine("{0},{1}", item.ProductName, item.ExtendedPrice);
}
{
var query = northwind.CustOrdersDetail(10284);
foreach (var item in query)
Console.WriteLine("{0},{1}", item.ProductName, item.ExtendedPrice);
}
SQL Server Profiler跟踪结果:

exec [dbo].[CustOrdersDetail] @OrderID=10284






评论: 0 | 引用: 0 | 查看次数: 5007
发表评论
请登录后再发表评论!