zoukankan      html  css  js  c++  java
  • C#函数与SQL储存过程

    一点点小认识作为memo,求指正。

    C#的函数SQL的储存过程有很多的相似性, 它们都是一段封闭的代码块,来提高代码的重用性,虽然现在复制粘贴很方便,但是我们在写多个函数的时候频繁的复制粘贴相同的内容会影响程序大小,那我在运 用相同运算的时候我们就要用函数或者储存过程,他们有着同样的目的,思路和出发点,但是很重要的一点是掌握他们的格式,

     大致了分为四类:

    C#函数写在 class大括号内,Main函数大括号外                             |      SQL储存过程写在指定数据库文件——可编程性——储存过程                        
                                                                                                     |
     定义函数 public/private/等等                                                          创建一个函数: Create @名称 数据类型                                                  
                                                                                                     |
    例:C#无返回值,无输入输出参数函数                                          |       SQL储存过程不带参数,不返回                                                              

     class hanshu                                                                               |   Create proc student
    {                                                                                                |      as
         static void hehe()                                                                    |            select * from student
    {                                                                                                      go
            Console.WriteLine("C#无返回值,无输入输出参数函数") ;           |         exec student --执行student存储过程,打印出student表,这两个student不同
    }                                                                                                |     一个是student表,一个是student储存过程
    static void Main(string [] args)                                                        |
       {                                                                                             |
             hehe();      //输出                                                                |
       }                                                                                             |
    }                                                                                                                     

    -----------------------------------------------------------------我是华丽的分割线------------------------------------------------------------------

    带参数不返回                                                                                        

    class hanshu                                                                             |     Create proc adt                               

    {                                                                                             |      @a int,

        public void hehe(string a, string b)                                           |      @b int,

           {                                                                                       |      @result int output

                  Console.WriteLine(a);                                                   |   as

                  Console.WriteLine(b);                                                   |      set    @result = @a +@b               

            }                                                                                      |   go

        static void Main (string [] args)                                                |    declare @anwer int

      {                                                                                            |    exec adt 1,5,@answer output

            hanshu outline = new hanshu();                                          |   select @answer

            outline.hehe ("睡你麻痹","起来嗨");                                       |

                                                                                                    |

             Console.ReadLine();                                                           |

      }                                                                                             |

    }                                                                                               |

    ----------------------------------------------------------------- 我是华丽的分割线 ------------------------------------------------------------------

    不带参数带返回值

    class hanshu                                                                              |    Create proc returncount
        {                                                                                          |    as
            public string  hehe()                                                            |      declare @a int
            {                                                                                       |       select @a=count(*) from student                 
                string s = "呵呵";                                                             |    return @a
                return s;                                                                        |    go
            }                                                                                       |    declare @a int
            static void Main(string[] args)                                                |   exec @a=returncount
            {                                                                                       |    print @a
                hanshu outline = new hanshu();                                       |
                string s = outline.hehe(); //这里两个s不一样                        |   --这里的两个@a也是不一样的
                Console.WriteLine(s);                                                       |                                      
                Console.ReadLine();                                                        |
            }                                                                                       |
        }                                                                                           |

    ----------------------------------------------------------------- 我是华丽的分割线 ------------------------------------------------------------------

    当然了以上3种情况我们用的不是非常多,毕竟一个成熟完整的函数或储存过程需要有输入参数和数出参数的,下面我们来简单地阐述下他们的格式

    C#                                                                                                  |  SQL

    class program                                                                                |    Create proc sss

                                                                                                      |   @a int,

         public int hanshu (int a, int b,........int n, out int c)                    |   @b int

    //public 引导了一个叫hanshu的函数,它有返回值且返回值是                     |   as

    整数,后括号里面写输入参数,以 , 隔开,out 后是数出参数                      |   储存过程内容

            {                                                                                           |   return

                 函数体                                                                                 |   go

                 return ; //返还一个int类型值给hanshu                                       |   exec--返回值=存储过程名 参数1,参数2,.......参数N  --执行

              }                                                                                           例:输入一个整数,比10小数出1,比10大100小数出2

     static void Main (string [] args)  //在主函数里调用                                 |其他情况输出-1

      {                                                                                                   |   create proc jugg

           program hs= new program(); //在program类中命名                       |   @a int

             一个叫hs的执行体并初始化;                                                       |   as

            hs.hanshu();//让hs函数体执行hanshu运作                                if @a>=0 and @a<10

        }                                                                                                |   begin

    }                                                                                                    |       return 1

    例:写一个函数,要求输入一个数组比对出最大值,最小值                       |   end

    并降序或升序排列                                                                            |         else if @a>=10 and @a<100

    class Program                                                                                 |   begin
        {                                                                                               |        return 2
            public int[] n(int[] n, out int a, out int b)                                    |    end
            {                                                                                           |   else
                 a = 0;                                                                               |   begin
                 b = 0;                                                                               |         return -1
                for (int i = 0; i < n.Length; i++)                                            |   end
                {                                                                                        |   go
                    for (int j = i; j < n.Length - 1; j++)      // j是两两比的次数;  |     declare @a int --阐述一int类型的@a 但@a跟上面不同
                    {                                                                                    |     exec @a=jugg 20
    --执行@a储存过程 输入20
                        if (n[i]<n[j+1])                                                            |     select @a     --打印出结果
                        {                                                                                |
                            int m = 0;                                                               |
                            m=n[i];                                                                   |
                            n[i] = n[j + 1];                                                         |
                            n[j+ 1] = m;                                                            |                    
                        }                                                                                |
                    }                                                                                    |
                }                                                                                        |
                a = n[0];                                                                             |
                b = n[n.Length - 1];                                                             |
                 return n;                                                                            |
                                                                                                          |
            }                                                                                             |
            static void Main(string[] args)                                                     |
            {                                                                                             |
                int a, b;                                                                               |
                Console.WriteLine("请输入数组位数");                                       |
                int x = Convert.ToInt32(Console.ReadLine());                         |
                int[]n=new int[x];                                                                |
                  Console.WriteLine("请输入一个数组,我来输出最大值,最小值"); |
                        for (int i = 0; i < x; i++)                                                |
                        {                                                                                |
                            Console.Write("第"+(i+1)+"个");                                |
                            n[i] = Convert.ToInt32(Console.ReadLine());              |
                        }                                                                               |
                        int[] n1 = new Program().n(n,out a,out b);                    |
                        for (int i = 0; i < x; i++)                                               |
                        {                                                                                |
                            Console.WriteLine("数组降序排列为:" + n[i]);               |                     
                        }                                                                                |
                        Console.WriteLine("并且数组最大值是:"+a+"最小值是"+b);   |
                                                                                                          |
                        Console.ReadLine();                                                     |
            }                                                                                           |
        }                                                                                               |

    小结:函数和储存过程都是方便我们使用的运算体,要注意我们的输入参数和返回参数,类型,数量等。通过做题加深印象。

  • 相关阅读:
    R安装程序包:ERROR: failed to lock directory ‘/usr/lib64/R/library’ for modifying
    clusterProfiler
    R 画图保存为文件
    网站支持https之一:https原理和SSL证书类型
    Https系列之一:https的简单介绍及SSL证书的生成
    What is the difference between a directory and a folder?
    漫画解读:通过造车来了解软件开发模式 DevOps
    Multicast delegate of type Func (with return value)?
    What are good ways to prevent SQL injection? [duplicate]
    SqlCommand.Prepare Method
  • 原文地址:https://www.cnblogs.com/18553325o9o/p/4459038.html
Copyright © 2011-2022 走看看