C# dapper 动态参数 DynamicParameters

来源:互联网 发布:绿岸网络很多人离职 编辑:程序博客网 时间:2024/06/09 18:12

 public static MesLjzpgyInfo Insert(MesLjzpgyInfo model)        {            StringBuilder strSql = new StringBuilder();            strSql.Append("insert into MesLjzpgy(");            strSql.Append("LJBM,GYFA,GYBC,SYBZ,GXRY,GXRQ)");            strSql.Append(" values (");            strSql.Append("@LJBM,@GYFA,@GYBC,@SYBZ,@GXRY,@GXRQ)");            using (var db = Database.DbService)            {                strSql.Append(";SELECT @returnid=SCOPE_IDENTITY()");                var p = new DynamicParameters(model);                p.Add("@returnid", dbType: DbType.Int32, direction: ParameterDirection.Output);                db.Execute(strSql.ToString(), p);                model.ID = p.Get<int>("@returnid");                 return model;            }        }
public static IDbConnection DbService        {            get            {                if (df == null)                    df = DbProviderFactories.GetFactory(ProviderFactoryString);                var connection = df.CreateConnection();                connection.ConnectionString = ConnectionString;                try                {                    connection.Open();                    return connection;                }                catch (Exception ex)                {                    throw new InvalidOperationException("打开数据库时发生错误,请保证数据库可以正常连接,确保web.config中ConnectionString正确配置。\n详细错误:" + ex.Message);                }             }        }
0 0