Reader()数据对象应该在里面创建

public GetQuotationbyIdJson GetQuotationbyId(string quotationid)
        {
            GetQuotationbyIdJson json = null;
            
            StringBuilder sqlstr = new StringBuilder();
            ArrayList list=new ArrayList();
            sqlstr.Append("select qp.SellerId,qp.QuotationId,qp.BookNo,qp.CarTypeName,qp.CarModelPKID,qp.CarColorId,");
            sqlstr.Append("qp.Qty,qp.Price,qp.PrePayMoney,qp.RemainMoney,qp.CASHAMT,");
            sqlstr.Append("qp.STAGEAMT,qp.PurchaseTax,");
            sqlstr.Append("qp.CarboatTax,qp.LicenceFee,qp.purchaseOther1,qp.purchaseOther2,");
            sqlstr.Append("qp.PurchaseTotal,qp.InsuranceTotal,qp.DamageTax,qp.CTALTax,");
            sqlstr.Append("qp.ThirdTax,qp.StealTax,qp.ResponseTax,qp.ScrachesTax,qp.GlassTax,");
            sqlstr.Append("qp.LimitTax,qp.FirstPer,qp.FirstPerPrice,qp.LoanPer,qp.LoanPerPrice,");
            sqlstr.Append("qp.LoanTerm,qp.PayPerMonth,qp.stageOther1,qp.stageOther2,qp.TOTALPRICE,qp.DeliveryDate,qp.estimateDate ");
            sqlstr.Append(", qp.PAYTYPE AS carPriceOtherPayType  ");
            sqlstr.Append("from ST_QuotePrice qp where qp.QuotationId= ? ");

            OleDbParameter[] parms = new OleDbParameter[1];
            parms[0] = CreateOleDbParameter("QuotationId", OleDbType.VarChar, 20, quotationid);

            using(IDataReader reader = ExecuteReader(sqlstr.ToString(),parms))
            {
                while(reader.Read())
                {        
                    json = new GetQuotationbyIdJson();
                    json.salerID=reader["SellerId"] == DBNull.Value ? "" : reader["SellerId"].ToString();
                    json.quotationId=reader["QuotationId"] == DBNull.Value ? "" : reader["QuotationId"].ToString();
                    json.customerID=reader["BookNo"] == DBNull.Value ? "" : reader["BookNo"].ToString();
                    json.carclass=reader["CarTypeName"] == DBNull.Value ? "" : reader["CarTypeName"].ToString();
                    json.cartype=reader["CarModelPKID"] == DBNull.Value ? "" : reader["CarModelPKID"].ToString();
                    json.carcolor=reader["CarColorId"] == DBNull.Value ? "" : reader["CarColorId"].ToString();
                    json.carNum = reader["Qty"] == DBNull.Value ? 0 : Convert.ToInt32(reader["Qty"]) ;
                    json.carPrice = reader["PRICE"] == DBNull.Value ? 0.0m : Convert.ToDecimal(reader["PRICE"]) ;
                    json.carPricePreAmount=reader["PrePayMoney"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["PrePayMoney"]); 
                    json.carPriceOtherAmount=reader["RemainMoney"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["RemainMoney"]);
                    json.carPriceOtherCash=reader["CASHAMT"] == DBNull.Value ? 0.00m : Convert.ToDecimal(reader["CASHAMT"]) ;
                    json.carPriceOtherBystage=reader["STAGEAMT"] ==  DBNull.Value ? 0.00m : Convert.ToDecimal(reader["STAGEAMT"]) ;
                    json.PurchaseRoadTax=reader["PurchaseTax"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["PurchaseTax"]);
                    json.PurchaseCarboatTax=reader["CarboatTax"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["CarboatTax"]);
                    json.PurchaseLicenceFee=reader["LicenceFee"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["LicenceFee"]);                    
                    json.purchaseOther1=reader["purchaseOther1"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["purchaseOther1"]);
                    json.purchaseOther2=reader["purchaseOther2"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["purchaseOther2"]);
                    json.purchaseTotal=reader["PurchaseTotal"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["PurchaseTotal"]);
                    json.insurance=reader["InsuranceTotal"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["InsuranceTotal"]);
                    json.insuranceDamageTax=reader["DamageTax"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["DamageTax"]);
                    json.CTALTax=reader["CTALTax"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["CTALTax"]);
                    json.insuranceThirdTax=reader["ThirdTax"] == DBNull.Value ?0.00m:Convert.ToDecimal(reader["ThirdTax"]);
                    json.addTaxSteal = reader["StealTax"] == DBNull.Value ? 0.00m : Convert.ToDecimal(reader["StealTax"]);
                    json.addResponseTax=reader["ResponseTax"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["ResponseTax"]);
                    json.addScrachesTax=reader["ScrachesTax"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["ScrachesTax"]);
                    json.addGlassTax=reader["GlassTax"] == DBNull.Value ?0.00m:Convert.ToDecimal(reader["GlassTax"]);
                    json.addLimitTax=reader["LimitTax"] == DBNull.Value ?0.00m:Convert.ToDecimal(reader["LimitTax"]);
                    json.stagePayFirstPer=reader["FirstPer"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["FirstPer"]);
                    json.stagePayfirstPrice=reader["FirstPerPrice"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["FirstPerPrice"]);
                    json.stagePayLoanPer=reader["LoanPer"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["LoanPer"]);
                    json.stagePayLoanPrice=reader["LoanPerPrice"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["LoanPerPrice"]);
                    json.stagePayTerm=reader["LoanTerm"] == DBNull.Value ? 0:Convert.ToInt32(reader["LoanTerm"]);
                    json.stagePayPerMonth=reader["PayPerMonth"] == DBNull.Value ? 0.00m:Convert.ToDecimal(reader["PayPerMonth"]);                    
                    json.stageOther1=reader["stageOther1"] ==DBNull.Value ? 0.00m:Convert.ToDecimal(reader["stageOther1"]);
                    json.stageOther2=reader["stageOther2"] ==DBNull.Value ? 0.00m:Convert.ToDecimal(reader["stageOther2"]);
                    json.quotationTotal=reader["TOTALPRICE"] ==DBNull.Value ? 0.00m:Convert.ToDecimal(reader["TOTALPRICE"]);
                    json.estimateDate=reader["estimateDate"] == DBNull.Value ? string.Empty : Convert.ToDateTime(reader["estimateDate"]).ToString("yyyy-MM-dd");                    
                    json.deliverytime=reader["DeliveryDate"] == DBNull.Value ? string.Empty : Convert.ToDateTime(reader["DeliveryDate"]).ToString("yyyy-MM-dd");
                    json.carPriceOtherPayType = reader["carPriceOtherPayType"] == DBNull.Value ? string.Empty : reader["carPriceOtherPayType"].ToString();
                }
            }            
            return json;
        }

如果在Reader()的外面

 GetQuotationbyIdJson json = new GetQuotationbyIdJson()那么系统返回的就会有个默认的初始化了的json,而这往往并不是我们所需要的
原文地址:https://www.cnblogs.com/ShaYeBlog/p/3031304.html