您现在的位置: 捷凌网安 >> 服务器 >> WEB开 发 >> ASP.NET >> 正文
ASP.NET 2.0:AdventureWorks系统分析

作者:佚名 责任编辑:左决 点击数: 更新时间:2008-2-28 10:02:48

   4.实现CLR存储过程

    SQL Server 2005最优雅的功能之一是集成.NET CLR。集成CLR的SQL Server在多个重要的方面扩展了SQL Server功能。利用这种集成可创建数据库对象(例如存储过程),用户定义函数,以及使用现代面向对象语言(例如VB.NET和C#)创建触发器。为达成本实例目的,将说明使用C#创建存储过程的方法。

    首先,在Visual Studio 2005中,以Visual Studio 2005作为编程语言创建一个新的名为AdventureWorksDatabaseObjects的SQL Server项目。由于创建的是数据库项目,所以需要关联数据源与项目。在创建项目的同时,Visual Studio将要求开发人员要么选择现有数据库引用,要么添加新数据库引用。选择AdventureWorks作为数据库。一旦创建项目,则在项目菜单中选择添加存储过程选项。在添加新项对话框中,输入StoredProcedures.cs,接着单击添加按钮。在创建类之后,可根据以下内容修改类中代码。

    示例1:实现CLR存储过程

   

 using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void GetProductCategories()
{
using (SqlConnection connection = new SqlConnection("context connection=true"))
{
connection.Open();
string sqlCommand = "Select ProductCategoryID, Name, rowguid, " +
" ModifiedDate from Production.ProductCategory";
SqlCommand command = new SqlCommand(sqlCommand, connection);
SqlDataReader reader = command.ExecuteReader();
SqlContext.Pipe.Send(reader);
}
}

[Microsoft.SqlServer.Server.SqlProcedure]
public static void GetProductSubcategories(int productCategoryID)
{
using (SqlConnection connection = new SqlConnection("context connection=true"))
{
connection.Open();
string sqlCommand = "Select ProductSubcategoryID, ProductCategoryID, Name," + "rowguid, ModifiedDate from Production.ProductSubcategory " + "Where ProductCategoryID = " + productCategoryID;
SqlCommand command = new SqlCommand(sqlCommand, connection);
SqlDataReader reader = command.ExecuteReader();
SqlContext.Pipe.Send(reader);
}
}

[Microsoft.SqlServer.Server.SqlProcedure]
public static void GetProducts(int productSubcategoryID)
{
using (SqlConnection connection = new SqlConnection("context connection=true"))
{
connection.Open();
string sqlCommand = "Select ProductID, Name, ProductNumber,MakeFlag, " + "FinishedGoodsFlag, Color, SafetyStockLevel, ReorderPoint,StandardCost, " + "ListPrice, Size, SizeUnitMeasureCode, WeightUnITMeasureCode," + "Weight,DaysToManufacture, ProductLine,Class, Style, " + "ProductSubcategoryID, ProductModelID,SellStartDate,SellEndDate," + "DiscontinuedDate, rowguid, ModifiedDate from Production.Product " + "Where ProductSubcategoryID = " + productSubcategoryID.ToString();
SqlCommand command = new SqlCommand(sqlCommand, connection);
SqlDataReader reader = command.ExecuteReader();
SqlContext.Pipe.Send(reader);
}
}
};

上一页  [1] [2] [3] [4] 下一页

  • 上一篇文章:

  • 下一篇文章:
  •  
    最进更新
    普通文章foxpro 更新源表05-05
    普通文章foxpro 让视图与数据源相连05-05
    普通文章foxpro 机动查询和数据输入05-05
    普通文章foxpro 多个本地数据05-05
    普通文章foxpro 维护源表05-05
    普通文章Oracle 10g Release2新功能之05-05
    普通文章将Oracle 10g内置的安全特性05-05
    普通文章ACCESS 2003 建立数据库视频05-05
    普通文章三种SQL分页法效率分析05-05
    普通文章优化MySQL数据库查询的三种方05-05
     
    推荐文章
    推荐文章教你怎样在MySQL中提高全文搜05-05
    推荐文章SQL Server中数据导入导出三05-05
    推荐文章缓冲技术提高JSP程序的性能和04-17
    推荐文章asp去除HTML标记的三个实用函04-17
    推荐文章何时使用DataGrid、DataList04-17
    推荐文章MySQL存储过程示例04-14
    推荐文章华硕搭建Exchange2007企业邮03-14
    推荐文章升级Win 2003到Windows 200803-14
    推荐文章windows Server 2003 搭建域03-14
    推荐文章服务器成为IT中心的6个理由03-11
     
    热点文章 
    普通文章SQL Server 2008分析服务概览05-01
    普通文章Dlink路由器VPN设置04-29
    推荐文章缓冲技术提高JSP程序的性能和04-17
    普通文章教你优化你的ASP程序04-17
    推荐文章asp去除HTML标记的三个实用函04-17
    普通文章ASP添加验证码的解决方法04-17
    推荐文章何时使用DataGrid、DataList04-17
    普通文章Asp.net中禁止用户多次登录04-17
    普通文章MySQL之表结构修改04-14
    推荐文章MySQL存储过程示例04-14

    | 设为首页 | 加入收藏 | 联系站长 | 广告服务 | 友情链接 | 版权申明 | 网站地图 |

    在线交流 捷凌网安主群:51649627
    Copyright 2007-2008 © 捷凌网安. All rights reserved.
    备案序号:蜀ICP备08001812号