通过Web服务从结果表中保存SQL数据库中的数据的问题

时间:2011-08-23 17:38:58

标签: c# asp.net sql web-services

我有问题,我需要你的帮助。正如我的Web服务描述的那样,我想获取一些数据并将它们添加到我的数据库中。

[WebMethod(Description = "This will input computers into the database", EnableSession = false)]
public string orderItem(int CUS_ID, string COM_ID, int Quantity,double COMPrice)
{

    try
    {

        dbConn = new DbConnection();
        SqlConnection conn = dbConn.OpenConnection();
        SqlCommand orderItem = new SqlCommand("OrderComputer", conn);
        orderItem.CommandType = CommandType.StoredProcedure;


        SqlParameter add_CUS_ID = orderItem.Parameters.Add("@CUS_ID", SqlDbType.Int, 4);
        add_CUS_ID.Value = CUS_ID;

        SqlParameter addBK_ISBN = orderItem.Parameters.Add("@COM_ID", SqlDbType.Char, 80);
        addBK_ISBN.Value = COM_ID;

        SqlParameter add_Quantity = orderItem.Parameters.Add("@Quantity", SqlDbType.Int, 2);
        add_Quantity.Value = Quantity;

        SqlParameter add_COMPrice = orderItem.Parameters.Add("@COMPrice", SqlDbType.Money, 8);
        add_COMPrice.Value = COMPrice;


        return this.ExecuteQuery(orderItem);
    }
    catch (Exception e)
    {
        return e.ToString();
    }
}

OrderComputer 是一个存储过程:

ALTER  Procedure OrderComputer
(
    @CUS_ID int,
    @COM_ID int,
    @Quantity int,
    @COMPrice money
)

AS

declare @Date datetime
declare @ShipDate datetime
declare @OR_ID int


select @Date = getdate()
select @ShipDate = getdate()

begin tran NewComputer


INSERT INTO Orders
(
    CUS_ID, 
    Date, 
    ShipDate
)

VALUES
(   
    @CUS_ID, 
    @Date, 
    @ShipDate 
)

SELECT @OR_ID = @@Identity 


INSERT INTO ComputerOrders
(
    OR_ID, 
    COM_ID, 
    Quantity, 
    COMPrice
)

VALUES
(
    @OR_ID,
    @COM_ID,
    @Quantity,
    @COMPrice
)

commit tran NewComputer

以下部分是我购物车的最后一步。它返回一个包含订单详细信息的表。我的问题是为什么行

order.orderItem(customerID, Computer_ID, quantity, price);

无法获取将其添加到数据库的记录?缺少什么?

computerOrder1.computerOrder order = new computerOrder1.computerOrder();

int quantity = 2;

XmlDocument customer_Order = ComputerCart.getCartDescription();

while (customer_Order.SelectNodes("//Computers").Count > 0)
{

    string Computer_ID = customer_Order.GetElementsByTagName("Computers").Item(0).SelectSingleNode("//com_id").InnerText;

   double price = double.Parse(customer_Order.GetElementsByTagName("Computers").Item(0).SelectSingleNode("//price").InnerText);

   string Model = customer_Order.GetElementsByTagName("Computers").Item(0).SelectSingleNode("//model").InnerText;

   order.orderItem(customerID, Computer_ID, quantity, price);
}

1 个答案:

答案 0 :(得分:0)

Juding的评论:

protected string ExecuteQuery(SqlCommand QueryObject) { 
   int queryResult = QueryObject.ExecuteNonQuery(); 
   if (queryResult != 0) { 
      return "Your request is CORRECT"; 
   } 
   else { 
      return "error: QueryResult= " + queryResult; 
   } 
}

您不仅需要您的SqlCommand,还需要您的连接

protected string ExecuteQuery(SqlCommand QueryObject, SqlConnection conn) { 
   try
   {
      conn.open();
      int queryResult = QueryObject.ExecuteNonQuery(); 
      if (queryResult != 0) { 
         return "Your request is CORRECT"; 
      } 
      else { 
         return "error: QueryResult= " + queryResult; 
      }
   }
   finally
   { 
      conn.close();
   }
}