• 一个简单的存储过程


    CREATE PROCEDURE p_out
    @a as int,
    @b as int,
    @c as int output
    AS
    set @c=@a+@b
    return '10'
    ---------------------
    .........
    SqlCommand cmd2=new SqlCommand("p_out",conn);
    cmd2.CommandType=CommandType.StoredProcedure;
    cmd2.Parameters.Add("@a",SqlDbType.Int);
    cmd2.Parameters.Add("@b",SqlDbType.Int);
    SqlParameter para_c=cmd2.Parameters.Add("@c",SqlDbType.Int);
    SqlParameter para_r=cmd2.Parameters.Add("returnvalue",SqlDbType.Int);
    cmd2.Parameters[0].Value=2;
    cmd2.Parameters[1].Value=3;
    para_c.Direction=ParameterDirection.Output;
    para_r.Direction=ParameterDirection.ReturnValue;
    conn.open();
    cmd2.ExecuteNonQuery();
    conn.close();
    this.Response.Write(para_c.value.ToString());
    this.Response.Write(para_r.value.ToString());

    ---------------------------------------------------------------------------------------------------

    CREATE PROCEDURE sp_AccountRole_Create
    @CategoryID int,
    @RoleName nvarchar(10),
    @Description nvarchar(50),
    @RoleID int outputAS   
    DECLARE @Count int
    SELECT @Count = Count(RoleID) FROM Account_Role WHERE RoleName = @RoleName
    IF @Count = 0
    INSERT INTO Account_Role (CategoryID, RoleName, Description) values
    (@CategoryID, @RoleName, @Description) SET @RoleID = @@IDENTITY
    RETURN 1
    GO

    执行存储过程的C#过程:
    SqlConnection DbConnection = new SqlConnection(mConnectionString);
    SqlCommand command = new SqlCommand( "sp_AccountRole_Create", DbConnection );
    DbConnection.Open(connectString);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add("@CategoryID", SqlDbType.Int, 4);
    command.Parameters.Add("@RoleName", SqlDbType.NVarChar, 10);
    command.Parameters.Add("@Description", SqlDbType.NVarChar, 50);
    command.Parameters.Add("@RoleID", SqlDbType.Int, 4);
    command.Parameters.Add("Returnvalue", SqlDbType.Int, 4, ParameterDirection.Returnvalue,false, 0,0,

    string.Empty,DataRowVersion.Default, null);
    command.parameters["@CategoryID"].value = permission.CategoryID;
    command.parameters["@RoleName"].value = permission.PermissionName;
    command.parameters["@Description"].value = permission.Description;
    command.parameters["@RoleID"].Direction = ParameterDirection.Output;
    int rowsAffected = command.ExecuteNonQuery();
    int result = command.parameters["Returnvalue"].value;
    int newID = command.parameters["@RoleID"].value;

  • 相关阅读:
    管理软件数据库备份策略
    融云群组同步策略
    Nginx中配置undertow进行两个项目的动静分离配置
    记录mysql正在执行的SQL语句
    RabbitMQ启动出现的问题与解决办法
    延迟任务的实现总结
    查找所有sphinx引擎表并生成创建表的语句
    go 学习笔记(4) --变量与常量
    go 学习笔记(4) import
    go 学习笔记(4) package
  • 原文地址:https://www.cnblogs.com/jcomet/p/1242539.html
Copyright © 2020-2023  润新知