一点点小认识作为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

{                                                        ,,@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();                                                     |
        }                                                                                           |
    }                                                                                               |

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

C#函数与SQL储存过程的更多相关文章

  1. sql储存过程in(多个参数)

    一.用sql函数 首先要创建一个截取字符串的函数,新建一个查询,把下面代码复制进去执行. 函数SqlitIn的第一个参数是储存过程要in的字符串,第二个参数是分隔符 CREATE function S ...

  2. sql 储存过程的使用

    --获取所有数据 根据自定义函数传人类型id返回类型名称 USE [Cloths] GO /****** Object: StoredProcedure [dbo].[Proc_all] Script ...

  3. 关于SQL储存过程中输出多行数据

    declare @num1 int           --为符合条件的总行数 select @num1=COUNT(1) from cardInfo where openDate between @ ...

  4. SQL储存过程

    基本语法 创建存储过程 create procedure sp_name @[参数名] [类型],@[参数名] [类型] as begin ......... end 以上格式还可以简写成: crea ...

  5. mysql的函数与储存过程与pymysql的配合使用

    现在mysql上定义一个函数,一个储存过程 函数: delimiter \\ CREATE FUNCTION f2 ( num2 INT, num1 INT ) RETURNS INT BEGIN D ...

  6. SQL SERVER 判断是否存在并删除某个数据库、表、视图、触发器、储存过程、函数

    -- SQL SERVER 判断是否存在某个触发器.储存过程 -- 判断储存过程,如果存在则删除IF (EXISTS(SELECT * FROM sysobjects WHERE name='proc ...

  7. MySQL 储存过程-原理、语法、函数详细说明

    Mysql储存过程是一组为了完成特定功能的SQL语句集,经过编译之后存储在数据库中,当需要使用该组SQL语句时用户只需要通过指定储存过程的名字并给定参数就可以调用执行它了,简而言之就是一组已经写好的命 ...

  8. 【转】oracle查询用户表,函数,储存过程,

    ◆Oracle查询用户表空间:select * from user_all_tables ◆Oracle查询所有函数和储存过程:select * from user_source ◆Oracle查询所 ...

  9. SQL获取所有数据库名、表名、储存过程以及参数列表

    SQL获取所有数据库名.表名.储存过程以及参数列表 1.获取所有用户名:SELECT name FROM Sysusers where status='2' and islogin='1'islogi ...

随机推荐

  1. 【leetcode】Minimum Depth of Binary Tree (easy)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  2. 【leetcode】Set Matrix Zeroes(middle)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...

  3. 【python】classmethod & staticmethod 区别

    来源:http://blog.csdn.net/carolzhang8406/article/details/6856817 其他参考: http://blog.csdn.net/lovingprin ...

  4. Windows下批处理执行MySQL脚本文件

    转载至http://my.oschina.net/u/660932/blog/117929 一. @echo offSetlocal enabledelayedexpansion::CODER BY ...

  5. Vim 强大的配置

    新建文件.vimrc,然后复制如下内容,并将该文件放到vim安装目录下 map <F9> :call SaveInputData()<CR> func! SaveInputDa ...

  6. Java代码简化神器-Lombok

    一.背景 前段时间在开源社区中发现了一个比较牛逼的简化Java代码的神器-Lombok,接着自己写了demo进行测试和练习,感觉真的很不错,特此分享给需要的小伙伴们~ 二.开发之前的准备 1.lomb ...

  7. endnote设置文献第二行悬挂缩进办法

    参考:http://blog.sina.com.cn/s/blog_62b13cf201014lfr.html  使用[endnote]插入文献后,如果文献稍长些,有第二行,第二行会顶格开始.并且这个 ...

  8. 《CLR via C#》读书笔记(1)CLR执行模型

    1.1 释义 CLR 公共语音运行时 Common Language Runtime CTS 通用类型系统 Common Type System CTS.CLS是CLR的核心 CLS 通用语言规范 C ...

  9. C#4.0图解教程 - 第24章 反射和特性 - 1.反射

    24.1 元数据和反射 有关程序及类型的数据被成为 元数据.他们保存在程序集中. 程序运行时,可以查看其他程序集或其本身的元数据.一个运行的程序查看本身元数据或其他程序的元数据的行为叫做 反射. 24 ...

  10. Redis笔记(七)Java实现Redis消息队列

    这里我使用Redis的发布.订阅功能实现简单的消息队列,基本的命令有publish.subscribe等. 在Jedis中,有对应的java方法,但是只能发布字符串消息.为了传输对象,需要将对象进行序 ...