C#写SQL SERVER2008存储过程

简介: 在C#中,选择"新建项目"->"数据库"->"SQL Server"->"Visual C# SQL CLR 数据库" 在项目中,单击右键选择"添加"->"存储过程",代码如下(SalesCrossTabs.

在C#中,选择"新建项目"->"数据库"->"SQL Server"->"Visual C# SQL CLR 数据库"

在项目中,单击右键选择"添加"->"存储过程",代码如下(SalesCrossTabs.cs):

 1 using System;
 2 using System.Data;
 3 using System.Data.SqlClient;
 4 using System.Data.SqlTypes;
 5 using Microsoft.SqlServer.Server;
 6 
 7 
 8 public partial class StoredProcedures
 9 {
10     [Microsoft.SqlServer.Server.SqlProcedure]
11     public static void GetSalesPerTerritoryByMonth(SqlDateTime StartDate,SqlDateTime EndDate)
12     {
13         // 在此处放置代码
14         SqlCommand command = new SqlCommand();
15         command.Connection = new SqlConnection("Context connection=true");
16         command.Connection.Open();
17 
18         string sql = "select DISTINCT Convert(char(7),h.OrderDate,120) As YYYY_MM " +
19             "FROM Sales.SalesOrderHeader h " +
20             "WHERE h.OrderDate BETWEEN @StartDate AND @EndDate "+
21             "ORDER BY YYYY_MM";
22         command.CommandText = sql.ToString();
23 
24         SqlParameter param = command.Parameters.Add("@StartDate", SqlDbType.DateTime);
25         param.Value = StartDate;
26         param = command.Parameters.Add("@EndDate", SqlDbType.DateTime);
27         param.Value = EndDate;
28 
29         SqlDataReader reader = command.ExecuteReader();
30 
31         System.Text.StringBuilder yearsMonths=new System.Text.StringBuilder();
32         while (reader.Read())
33         {
34             yearsMonths.Append("["+(string)reader["YYYY_MM"]+"],");
35         }
36         //close the reder
37         //MessageBox.Show(yearsMonths.ToString());
38         reader.Close();
39 
40         if (yearsMonths.Length > 0)
41         {
42             yearsMonths.Remove(yearsMonths.Length - 1, 1);
43         }
44         else
45         {
46             command.CommandText =
47                 "RAISEERROR('No date present for the input date range.',16,1)";
48             try
49             {
50                 SqlContext.Pipe.ExecuteAndSend(command);
51             }
52             catch
53             {
54                 return;
55             }
56         }
57 
58         //Define the cross-tab query
59         sql =
60             "Select TerritoryId," +
61                 yearsMonths.ToString() +
62             "From " +
63             "(" +
64                 "SELECT  " +
65                     "TerritoryId, " +
66                     "CONVERT(char(7),h.OrderDate,120) AS YYYY_MM, " +
67                     "d.LineTotal " +
68                 "From Sales.SalesOrderHeader h " +
69                 "JOIN Sales.SalesOrderDetail d " +
70                     " ON h.SalesOrderID=d.SalesOrderID " +
71                 " WHERE h.OrderDate between @StartDate AND @EndDate " +
72             ") p " +
73             "PIVOT " +
74             "( " +
75                 "SUM (LineTotal) " +
76                 "FOR YYYY_MM IN " +
77                 "( " +
78                     yearsMonths.ToString() +
79                 ") " +
80             ") As pvt " +
81             "ORDER BY TerritoryId";
82 
83         //set the CommandText
84         command.CommandText = sql.ToString();
85 
86         //have the caller execute the cross-tab query
87         SqlContext.Pipe.ExecuteAndSend(command);
88 
89         //close the connection
90         command.Connection.Close();
91     }
92 };

生成并部署此存储过程,在Test.sql中写测试语句
USE AdventureWorks; 
Exec GetSalesPerTerritoryByMonth @StartDate='20070501',@EndDate='20070701';

 

 

相关文章
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第18天】SQL Server 存储过程具有提高性能、增强安全性、代码复用和易于维护等优点。它可以减少编译时间和网络传输开销,通过权限控制和参数验证提升安全性,支持代码共享和复用,并且便于维护和版本管理。然而,存储过程也存在可移植性差、开发和调试复杂、版本管理问题、性能调优困难和依赖数据库服务器等缺点。使用时需根据具体需求权衡利弊。
410 1
|
11月前
|
存储 SQL 数据库连接
C#程序调用Sql Server存储过程异常处理:调用存储过程后不返回、不抛异常的解决方案
本文分析了C#程序操作Sql Server数据库时偶发的不返回、不抛异常问题,并提出了解决思路。首先解析了一个执行存储过程的函数`ExecuteProcedure`,其功能是调用存储过程并返回影响行数。针对代码执行被阻塞但无异常的情况,文章总结了可能原因,如死锁、无限循环或网络问题等。随后提供了多种解决方案:1) 增加日志定位问题;2) 使用异步操作提升响应性;3) 设置超时机制避免阻塞;4) 利用线程池分离主线程;5) 通过信号量同步线程;6) 监控数据库连接状态确保可用性。这些方法可有效应对数据库操作中的潜在问题,保障程序稳定性。
790 11
|
存储 SQL 缓存
SQL Server存储过程的优缺点
【10月更文挑战第22天】存储过程具有代码复用性高、性能优化、增强数据安全性、提高可维护性和减少网络流量等优点,但也存在调试困难、移植性差、增加数据库服务器负载和版本控制复杂等缺点。
641 1
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第17天】SQL Server 存储过程是预编译的 SQL 语句集,存于数据库中,可重复调用。它能提高性能、增强安全性和可维护性,但也有可移植性差、开发调试复杂及可能影响数据库性能等缺点。使用时需权衡利弊。
340 3
|
存储 SQL 数据库
Sql Server 存储过程怎么找 存储过程内容
Sql Server 存储过程怎么找 存储过程内容
818 1
|
存储 SQL 数据库
SQL Server 临时存储过程及示例
SQL Server 临时存储过程及示例
236 3
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
SQL 网络协议 数据库连接
已解决:连接SqlServer出现 provider: Shared Memory Provider, error: 0 - 管道的另一端上无任何进程【C#连接SqlServer踩坑记录】
本文介绍了解决连接SqlServer时出现“provider: Shared Memory Provider, error: 0 - 管道的另一端上无任何进程”错误的步骤,包括更改服务器验证模式、修改sa用户设置、启用TCP/IP协议,以及检查数据库连接语句中的实例名是否正确。此外,还解释了实例名mssqlserver和sqlserver之间的区别,包括它们在默认设置、功能和用途上的差异。
|
7月前
|
XML 前端开发 C#
C#编程实践:解析HTML文档并执行元素匹配
通过上述步骤,可以在C#中有效地解析HTML文档并执行元素匹配。HtmlAgilityPack提供了一个强大而灵活的工具集,可以处理各种HTML解析任务。
340 19
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
383 3