C#存储过程的循序渐进

开发 后端
本文由浅入深地介绍了C#存储过程,希望对大家有所帮助。

①为什么要使用C#存储过程?

因为它比SQL语句执行快.

②C#存储过程是什么?

把一堆SQL语句罗在一起,还可以根据条件执行不通SQL语句.(AX写作本文时观点)

③来一个最简单的存储过程

  1. CREATE PROCEDURE dbo.testProcedure_AX  
  2. AS 
  3. select userID from USERS order by userid desc 

注:dbo.testProcedure_AX是你创建的存储过程名,可以改为:AXzhz等,别跟关键字冲突就行了.AS下面就是一条SQL语句,不会写SQL语句的请回避.

④我怎么在ASP.Net中调用这个存储过程?

  1. public static string GetCustomerCName(ref ArrayList arrayCName,ref ArrayList arrayID)  
  2.         {  
  3.             SqlConnection con=ADConnection.createConnection();  
  4.             SqlCommand cmd=new SqlCommand("testProcedure_AX",con);  
  5.             cmd.CommandType=CommandType.StoredProcedure;  
  6.             con.Open();  
  7.             try 
  8.             {  
  9.                 SqlDataReader dr=cmd.ExecuteReader();  
  10.                 while(dr.Read())  
  11.                 {  
  12.                     if(dr[0].ToString()=="")  
  13.                     {  
  14.                         arrayCName.Add(dr[1].ToString());  
  15.                     }  
  16.                 }  
  17.                 con.Close();   
  18.                 return "OK!";  
  19.             }  
  20.             catch(Exception ex)  
  21.             {  
  22.                 con.Close();  
  23.                 return ex.ToString();  
  24.             }  
  25.         }  

注:其实就是把以前

  1. SqlCommand cmd=new SqlCommand("select userID from USERS order by userid desc",con);  

中的SQL语句替换为存储过程名,再把cmd的类型标注为CommandType.StoredProcedure(存储过程)

⑤写个带参数的C#存储过程吧,上面这个简单得有点惨不忍睹,不过还是蛮实用的.

参数带就带两,一个的没面子,太小家子气了.

  1. CREATE PROCEDURE dbo.AXzhz  
  2. /*  
  3. 这里写注释  
  4. */  
  5. @startDate varchar(16),  
  6. @endDate varchar(16)   
  7. AS 
  8. select id   from table_AX where commentDateTime>@startDate and commentDateTime< @endDate order by contentownerid DESC 
  9.  

注:@startDate varchar(16)是声明@startDate 这个变量,多个变量名间用【,】隔开.后面的SQL就可以使用这个变量了.

⑥我怎么在ASP.Net中调用这个带参数的存储过程?

  1. public static string GetCustomerCNameCount(string startDate,string endDate,ref DataSet ds)  
  2. {  
  3.              SqlConnection con=ADConnection.createConnection();  
  4. //-----------------------注意这一段--------------------------------------------------------------------------------------------------------  
  5.              SqlDataAdapter da=new SqlDataAdapter("AXzhz",con);  
  6.              para0=new SqlParameter("@startDate",startDate);  
  7.              para1=new SqlParameter("@endDate",endDate);  
  8.              da.SelectCommand.Parameters.Add(para0);  
  9.              da.SelectCommand.Parameters.Add(para1);  
  10.              da.SelectCommand.CommandType=CommandType.StoredProcedure;  
  11. //-------------------------------------------------------------------------------------------------------------------------------  
  12.  
  13.              try 
  14.              {  
  15.                  con.Open();  
  16.                  da.Fill(ds);  
  17.                  con.Close();  
  18.                  return "OK";  
  19.              }  
  20.              catch(Exception ex)  
  21.              {  
  22.                  return ex.ToString();  
  23.              }              
  24.          }  
  25.  

注:把命令的参数添加进去,就OK了

鸟的,改字体颜色的东西太垃圾了,改不好,大家凑活着看.

⑦我还想看看SQL命令执行成功了没有.

注意看下面三行红色的语句

  1. CREATE PROCEDURE dbo.AXzhz  
  2. /*  
  3.    @parameter1 用户名  
  4.    @parameter2 新密码  
  5. */  
  6. @password nvarchar(20),  
  7. @userName nvarchar(20)  
  8. AS 
  9. declare @err0 int 
  10. update WL_user set password=@password where UserName=@userName  
  11. set @err0=@@error   
  12. select   @err0 as err0  
  13.  

注:先声明一个整型变量@err0,再给其赋值为@@error(这个是系统自动给出的语句是否执行成功,0为成功,其它为失败),最后通过select把它选择出来,某位高人说可以通过Return返回,超出本人的认知范围,俺暂时不会,以后再补充吧

⑧那怎么从后台获得这个执行成功与否的值呢?

下面这段代码可以告诉你答案:

 

  1. public static string GetCustomerCName()  
  2.         {  
  3.             SqlConnection con=ADConnection.createConnection();  
  4.              
  5.             SqlCommand cmd=new SqlCommand("AXzhz",con);  
  6.             cmd.CommandType=CommandType.StoredProcedure;  
  7.             para0=new SqlParameter("@startDate","2006-9-10");  
  8.             para1=new SqlParameter("@endDate","2006-9-20");  
  9.             da.SelectCommand.Parameters.Add(para0);  
  10.             da.SelectCommand.Parameters.Add(para1);   
  11.             con.Open();  
  12.             try 
  13.             {  
  14.                Int32 re=(int32)cmd.ExecuteScalar();   
  15.                 con.Close();   
  16.                 if (re==0)  
  17.                  return "OK!";  
  18.                 else 
  19.                  return "false";  
  20.             }  
  21.             catch(Exception ex)  
  22.             {  
  23.                 con.Close();  
  24.                 return ex.ToString();  
  25.             }  
  26.         }  

注:就是通过SqlCommand的ExecuteScalar()方法取回这个值,这句话是从MSDN上找的,俺认为改成:

      int re=(int)cmd.ExecuteScalar();   99%正确,现在没时间验证,期待您的测试!!!

⑨我要根据传入的参数判断执行哪条SQL语句!!~

下面这个存储过程可以满足我们的要求,竟然是Pascal/VB的写法,Begin----End ,不是{},,,对使用C#的我来说,这个语法有点恶心.........

  1. ALTER PROCEDURE dbo.selectCustomerCNameCount  
  2. @customerID int 
  3. AS 
  4. if @customerID=-1  
  5. begin 
  6. select contentownerid ,userCName,count(*) as countAll from view_usercomment group by contentownerid,userCName order by contentownerid DESC 
  7. end 
  8. else 
  9. begin 
  10. select contentownerid ,userCName,count(*) as countAll from view_usercomment where contentownerid=@customerID group by contentownerid,userCName order by contentownerid DESC 
  11. end 

好了,C#存储过程就给大家介绍到这里,算是抛砖引玉吧!还有更多东西等着我们去发现,无尽的征途!

【编辑推荐】

  1. C#调用VC DLL接口函数参数类型转换方法介绍
  2. 解决C#中用Oracle执行存储过程返回DataSet的问题
  3. C#线程同步技术之Monitor
  4. C#线程同步与死锁
  5. C#线程:线程池和文件下载服务器

 

责任编辑:book05 来源: cnblogs
相关推荐

2009-08-26 14:25:46

C#消息

2019-06-25 09:02:44

加密加密算法密钥

2011-05-20 10:39:43

oracle

2010-12-28 16:49:05

2012-03-01 22:37:02

Linux入门

2011-05-24 13:47:25

程序员

2023-03-23 08:49:39

负载均衡服务器

2020-02-19 08:48:04

Java内存模型CPU

2022-04-21 14:03:54

开发API生命周期

2010-12-31 15:28:41

Windows 7

2010-12-28 16:38:16

Windows SerWindows 部署服

2009-10-30 14:43:24

宽带接入网

2018-02-05 15:30:01

MariaDB服务器主从复制

2022-03-31 06:23:43

自动化响应网络安全

2020-07-17 10:37:08

云计算安全IT

2022-04-29 11:27:26

循序渐进!开展零信任

2020-11-23 11:09:18

大数据教育云计算

2010-01-06 16:40:30

cisco交换机vla

2009-08-13 17:58:34

C#存储过程

2009-08-04 10:20:22

C#源码存储过程
点赞
收藏

51CTO技术栈公众号