FreeSql icon indicating copy to clipboard operation
FreeSql copied to clipboard

存储过程、参数化、Output 使用总结

Open 2881099 opened this issue 5 years ago • 4 comments

1、fsql.Ado.方法

var ps = new[]
{
    new SqlParameter("@TableName", "tb1"),
    new SqlParameter("@FInterID", System.Data.SqlDbType.Int)
};
//new SqlParameter 切换数据库不方便
ps[1].Direction = System.Data.ParameterDirection.Output;
fsql.Ado.ExecuteNonQuery(System.Data.CommandType.StoredProcedure, "dbo.GetICMaxNum", ps);
Console.WriteLine(ps[1].Value);

2、fsql.Ado.CommandFluent

DbParameter p2 = null; //Output
fsql.Ado.CommandFluent("dbo.GetICMaxNum")
    .CommandType(CommandType.StoredProcedure)
    .WithParameter("TableName", "tb1")
    .WithParameter("FInterID", null, p =>
    {
        p2 = p;
        p.DbType = DbType.Int32;
        p.Direction = ParameterDirection.Output;
    })
    .ExecuteNonQuery();
Console.WriteLine(p2.Value);

3、其他姿势

DbParameter p3 = null;
fsql.Ado.CommandFluent("dbo.GetICMaxNum", new Dictionary<string, object>
    {
        ["TableName"] = "tb1"
        // 更多参数
    })
    .WithParameter("FInterID", null, p =>
    {
        p3 = p;
        p.DbType = DbType.Int32;
        p.Direction = ParameterDirection.Output;
    })
    .CommandType(CommandType.StoredProcedure)
    .ExecuteNonQuery();
Console.WriteLine(p3.Value);

2881099 avatar Nov 30 '20 11:11 2881099

mysql 按着配置的会提示 Input string was not in a correct format.

liuyl1992 avatar Dec 30 '20 10:12 liuyl1992

@id ?id 这两种前缀都试试

2881099 avatar Dec 30 '20 10:12 2881099

@id ?id 这两种前缀都试试

只是第一种方式不可以,但是fluent方式可以

liuyl1992 avatar Jan 01 '21 03:01 liuyl1992

如何使用表值参数啊?

lylezhan avatar Jul 13 '21 06:07 lylezhan