天天看点

C# 调用存储过程

这两天做个新项目,但是要使用第三方提供的程序,在读程序的过程中发现第三方的源码中使用了大量的存储过程,有些地方不太明白,通过在网上搜索,终于把存储过程这部分的调用搞明白了。下面是对C#使用存储过程的一个概述。

1.为什么要使用存储过程?

因为它比SQL语句执行快。以前在做开发的时候没有注意到这点,现在想想,确实是存储过程的执行速度要比SQL语句快,效率也更高

2.存储过程是什么?

把一堆SQL语句放在一起,还可以根据条件执行不同SQL语句。(个人理解)

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

CREATE PROCEDURE test

AS

select userID from USERS order by userid desc

注:test是你创建的存储过程名,可以改为:asdf等,别跟关键字冲突就行了。AS下面就是一条SQL语句,如果不会写SQL语句,那!@#¥%……

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

红色语句是关键

        public string TestPro()

        {

            SqlConnection conn = DBConnection.createConnection();

            SqlCommand cmd = new SqlCommand("test",conn); //test为存储过程名

            cmd.CommandType = CommandType.StoredProcedure;

            conn.Open();

            return cmd.ExecuteScalar().ToString();

        }

注:其实就是把以前

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

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

这其中,C#只是告诉数据库要使用哪个存储过程,至于怎么执行存储过程,那就是数据库的事了。

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

CREATE PROCEDURE testDate

@startDate varchar(16),

@endDate varchar(16) 

 select id  from userID where createDate >= @startDate and createDate =< @endDate order by id DESC

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

6.如何在ASP.NET中调用这个带参数的存储过程?

 public static string TestDatePro(string startDate,string endDate)

{

            //-----------------------注意这一段-----------

            SqlDataAdapter da = new SqlDataAdapter("testDate",conn);

            para0 = new SqlParameter("@startDate",startDate);

            para1 = new SqlParameter("@endDate",endDate);

            da.SelectCommand.Parameters.Add(para0);

            da.SelectCommand.Parameters.Add(para1);

            da.SelectCommand.CommandType=CommandType.StoredProcedure;

            //--------------------------------------------

            try

            {

                conn.Open();

                da.Fill(ds);

                conn.Close();

                return "OK";

            }

            catch(Exception ex)

                return ex.ToString();

            }            

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

7.如何知道SQL命令执行成功了没有

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

CREATE PROCEDURE dbo.testUser

/*

  @parameter1 用户名

  @parameter2 新密码

*/

@password nvarchar(20),

@userName nvarchar(20)

declare @err0 int

set @err0=@@error 

select  @err0 as err0

注:先声明一个整型变量@err0,再给其赋值为@@error(这个是系统自动给出的语句是否执行成功,0为成功,其它为失败),最后通过select把它选择出来。不知道可不可以使用return返回,有时间得试试

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

看了下面的代码,你就知道了

    public static string TestUserPro()

            SqlCommand cmd=new SqlCommand("AXzhz",conn);

            cmd.CommandType=CommandType.StoredProcedure;

            para0=new SqlParameter("@startDate","2006-9-10");

            para1=new SqlParameter("@endDate","2006-9-20");

            da.SelectCommand.Parameters.Add(para1); 

            conn.Open();

            try

               Int32 re=(int32)cmd.ExecuteScalar(); 

                conn.Close(); 

                if (re==0)

                 return "OK!";

                else

                 return "false";

注:就是通过SqlCommand的ExecuteScalar()方法取回这个值,这句话是从MSDN上找的,我认为改成:int re=(int)cmd.ExecuteScalar();  也是正确的。

9.我要根据传入的参数判断执行哪条SQL语句

下面这个存储过程可以满足我们的要求。

ALTER PROCEDURE testSelectPro

@customerID int

if(@customerID == -1)

 select contentownerid ,userCName,count(*) as countAll from view_usercomment group by contentownerid,userCName order by contentownerid DESC

}

else

本文转自 sw840227 51CTO博客,原文链接:http://blog.51cto.com/jerrysun/151463,如需转载请自行联系原作者

下一篇: JDK安装