zoukankan      html  css  js  c++  java
  • 存储过程从入门到熟练(多个存储过程完整实例及调用方法)_AX 转载

    存储过程从入门到熟练(多个存储过程完整实例及调用方法)_AX

    ①为什么要使用存储过程?
    因为它比SQL语句执行快.

    ②存储过程是什么?
    把一堆SQL语句罗在一起,还可以根据条件执行不通SQL语句.(AX写作本文时观点)

    ③来一个最简单的存储过程
    CREATE PROCEDURE dbo.testProcedure_AX
    AS
    select userID from USERS order by userid desc

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

    ④我怎么在ASP.NET中调用这个存储过程?
    下面黄底的这两行就够使了.
            public static string GetCustomerCName(ref ArrayList arrayCName,ref ArrayList arrayID)
            {
                SqlConnection con=ADConnection.createConnection();
                SqlCommand cmd=new SqlCommand("testProcedure_AX",con);
                cmd.CommandType=CommandType.StoredProcedure;

                con.Open();
                try
                {
                    SqlDataReader dr=cmd.ExecuteReader();
                    while(dr.Read())
                    {
                        if(dr[0].ToString()=="")
                        {
                            arrayCName.Add(dr[1].ToString());
                        }
                    }
                    con.Close();
                    return "OK!";
                }
                catch(Exception ex)
                {
                    con.Close();
                    return ex.ToString();
                }
            }
    注:其实就是把以前
    SqlCommand cmd=new SqlCommand("select userID from USERS order by userid desc",con);
    中的SQL语句替换为存储过程名,再把cmd的类型标注为CommandType.StoredProcedure(存储过程)

    ⑤写个带参数的存储过程吧,上面这个简单得有点惨不忍睹,不过还是蛮实用的.
    参数带就带两,一个的没面子,太小家子气了.

    CREATE PROCEDURE dbo.AXzhz
    /*
    这里写注释
    */
    @startDate varchar(16),
    @endDate varchar(16)
    AS
     select id  from table_AX where commentDateTime>@startDate and commentDateTime<@endDate order by contentownerid DESC

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

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

     public static string GetCustomerCNameCount(string startDate,string endDate,ref DataSet ds)
    {
                SqlConnection con=ADConnection.createConnection();
    //-----------------------注意这一段--------------------------------------------------------------------------------------------------------
                SqlDataAdapter da=new SqlDataAdapter("AXzhz",con);

                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
                {
                    con.Open();
                    da.Fill(ds);
                    con.Close();
                    return "OK";
                }
                catch(Exception ex)
                {
                    return ex.ToString();
                }            
            }

    注:把命令的参数添加进去,就OK了
    鸟的,改字体颜色的东西太垃圾了,改不好,大家凑活着看.

    ⑦我还想看看SQL命令执行成功了没有.
    注意看下面三行红色的语句

    CREATE PROCEDURE dbo.AXzhz
    /*
      @parameter1 用户名
      @parameter2 新密码
    */
    @password nvarchar(20),
    @userName nvarchar(20)
    AS
    declare @err0 int
    update WL_user set password=@password where UserName=@userName
    set @err0=@@error
    select  @err0 as err0

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

    ⑧那怎么从后台获得这个执行成功与否的值呢?
    下面这段代码可以告诉你答案:
        public static string GetCustomerCName()
            {
                SqlConnection con=ADConnection.createConnection();
               
                SqlCommand cmd=new SqlCommand("AXzhz",con);
                cmd.CommandType=CommandType.StoredProcedure;
                para0=new SqlParameter("@startDate","2006-9-10");
                para1=new SqlParameter("@endDate","2006-9-20");
                da.SelectCommand.Parameters.Add(para0);
                da.SelectCommand.Parameters.Add(para1); 
                con.Open();
                try
                {
                   Int32 re=(int32)cmd.ExecuteScalar();
                    con.Close();
                    if (re==0)
                     return "OK!";
                    else
                     return "false";
                }
                catch(Exception ex)
                {
                    con.Close();
                    return ex.ToString();
                }
            }
    注:就是通过SqlCommand的ExecuteScalar()方法取回这个值,这句话是从MSDN上找的,俺认为改成:
         int re=(int)cmd.ExecuteScalar();  99%正确,现在没时间验证,期待您的测试!!!

    ⑨我要根据传入的参数判断执行哪条SQL语句!!~
    下面这个存储过程可以满足我们的要求,竟然是Pascal/VB的写法,Begin----End ,不是{},,,对使用C#的我来说,这个语法有点恶心.........

    ALTER PROCEDURE dbo.selectCustomerCNameCount
    @customerID int
    AS
    if @customerID=-1
     begin
     select contentownerid ,userCName,count(*) as countAll from view_usercomment group by contentownerid,userCName order by contentownerid DESC
     end
    else
     begin

     select contentownerid ,userCName,count(*) as countAll from view_usercomment where contentownerid=@customerID group by contentownerid,userCName order by contentownerid DESC
     end

    好了,俺的水平只止于此,也够菜鸟们喝一壶的了,还有更多东西等着我们去发现,无尽的征途!!!!!!!!!!!

    posted on 2006-09-20 20:15 斧头帮少帮主 阅读(1729) 评论(4)  编辑 收藏 引用 网摘 所属分类: 经验总结

    评论

    # re: 存储过程从入门到熟练(多个存储过程完整实例及调用方法)_AX 2006-09-27 21:13 斧头帮少帮主

    【追加】
    有了上面的基础,我们就可以更进一步了!
    我要在存储过程中加入循环,通过条件不同来执行不同的操作.
     
    CREATE PROCEDURE dbo.AX
    @UserID int,
    @stlID varchar(32)            /*一个字符串,其中通过含有n个"|"*/
    AS
    declare @stID int
    declare @sstlID varchar(4) /*声明一个变量*/
    set @stID=0
    /*搜索字符串,看是否还含有"|"*/
    while(patindex('%|%',@stlID)<>0)
    begin
                 /*取出@stlID中第一个"|"前的字符,赋值给@sstlID*/
     set @sstlID=substring(@stlID,1,patindex('%|%',@stlID)-1)
     /*通过查询结果作为下面语句执行的判断条件*/
     if exists(select stID from WL_User_TypeTool where UserID=@UserID and stID=@stID)
     begin
     /*更新記錄*/
     Update WL_User_TypeTool set stlID=@sstlID where UserID=@UserID and stID=@stID
     end
     
     else
     begin
     /*插入記錄*/
     Insert into WL_User_TypeTool(UserID,stID,stlID) values (@UserID,@stID,@sstlID)
     end
     set @stID=@stID+1
    /*去掉@stlID最前一个"|"前面的部分,一次循环完成*/
     set @stlID=substring(@stlID,patindex(('%|%'),@stlID)+1,len(@stlID))
    end
     
    注:SQL里字符串索引值是从1开始的
    ②为变量赋值前面记得要有set关键字,我在这上面吃过亏!!
    关于patindex()方法的使用,请参见:
    http://www.cnblogs.com/AXzhz/archive/2006/08/19/481172.html
      回复  更多评论   
  • 相关阅读:
    Django forms组件
    Django 分页器
    Django Ajax
    Django 多表操作2
    js12种应该注意的地方
    Web自动化测试python环境中安装 --selenium安装、火狐和火狐驱动版本、谷歌和谷歌驱动版本、测试
    python学习-文件操作
    关于redis搭建环境
    扩展知识
    javascript之Banner图片焦点轮播
  • 原文地址:https://www.cnblogs.com/zyf/p/585102.html
Copyright © 2011-2022 走看看