当前位置: 首页 > 工具软件 > FreeSQL > 使用案例 >

Freesql中使用存储过程

万俟飞语
2023-12-01

Freesql中使用存储过程

public string SqlStoredProcedure(string guid, string Userid, string Erpin_id, string Flag_force)
        {
            DbParameter out1 = null; DbParameter out2 = null; DbParameter out3 = null; DbParameter out4 = null; DbParameter out5 = null; DbParameter out6 = null;
            DbParameter out7 = null; DbParameter out8 = null; DbParameter out9 = null; DbParameter out10 = null; DbParameter out11 = null; DbParameter out12 = null;
            DbParameter out13 = null; DbParameter out14 = null; DbParameter out15 = null; DbParameter out16 = null; DbParameter out17 = null; DbParameter out18 = null;
            DbParameter out19 = null; DbParameter out20 = null; DbParameter reason = null; 
            var sadasd2=_fsql.Ado.CommandFluent("sp_rf_in_close_ordno")
                .CommandType(CommandType.StoredProcedure)
                .CommandTimeout(60)

                .WithParameter("@guid", guid)
                .WithParameter("@in1", Userid)
                .WithParameter("@in2", Erpin_id)
                .WithParameter("@in3", Flag_force)
                .WithParameter("@in4", null)
                .WithParameter("@in5", null)
                .WithParameter("@in6", null)
                .WithParameter("@in7", null)
                .WithParameter("@in8", null)
                .WithParameter("@in9", null)
                .WithParameter("@in10", null)
                .WithParameter("@out1", 500, p =>
                {
                    out1 = p; //Output 参数
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out2", 500, p =>
                {
                    out2 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out3", 500, p =>
                {
                     out3 = p;
                     p.DbType = DbType.String;
                     p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out4", 500, p =>
                {
                    out4 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out5", 500, p =>
                {
                    out5 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out6", 500, p => 
                {
                    out6 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out7", 500, p => {
                    out7 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out8", 500, p =>
                {
                    out8 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out9", 500, p =>
                {
                    out9 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out10", 500, p =>
                {
                    out10 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out11", 500, p => 
                {
                    out11 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out12", 500, p =>
                {
                    out12 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out13", 500, p =>
                {
                    out13 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out14", 500, p =>
                {
                    out14 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out15", 500, p =>
                {
                    out15 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out16", 500, p =>
                {
                    out16 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out17", 500, p =>
                {
                    out17 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out18", 500, p =>
                {
                    out18 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out19", 500, p =>
                {
                    out19 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@out20", 500, p =>
                {
                    out20 = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .WithParameter("@reason", 500, p=> 
                {
                    reason = p;
                    p.DbType = DbType.String;
                    p.Direction = ParameterDirection.Output;
                })
                .ExecuteDataTable();
            var fanhui = reason.Value;
            return (string)fanhui;
        }

拿代码块来说一下是怎么样用的,这个在Freesql的官网上也有写用法。
官方实例:

DbParameter p2 = null;
fsql.Ado.CommandFluent("dbo.GetICMaxNum")
    .CommandType(CommandType.StoredProcedure)
    .CommandTimeout(60)

    .WithParameter("TableName", "tb1")
    .WithParameter("FInterID", null, p =>
    {
        //(p as OracleParameter).OracleType = ...;
        p2 = p; //Output 参数
        p.DbType = DbType.Int32;
        p.Direction = ParameterDirection.Output;
    })
    .ExecuteNonQuery(); //.Query<T>() 或者 .ExecuteDataTable() 或者 ...

Console.WriteLine(p2.Value);

上面是输入参数,我这边是通过传值来当输入参数的,这个没什么问题。当时我是在百度上找的代码,输出参数并没有得到接收,所以一直在报错,后来翻了一下官网找到了上述写法。
这边有个问题需要注意一下,我们这边接收存储过程的返回值,是个object类型,取不到报错信息提示,于是我这边直接改动的存储过程,在所有的报错提示前都加了序号,然后在后端代码里对取到的序号进行判断。

 类似资料: