C#dapper動的パラメータDynamicParameters

3035 ワード


 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    。
:"
 + ex.Message);                 }             }         }