如何从Web应用程序调用Web服务的更新功能

本文关键字:Web 更新 新功能 服务 应用程序 调用 | 更新日期: 2023-09-27 17:55:29

我有一个Web服务函数,用于更新我的数据库。我想从我的 Web 应用程序调用它,但它不起作用。在调用我的 Web 服务后,它可以通过我的 Web 服务进行更新,但不能通过我的 Web 应用程序进行更新。我调用服务函数的代码是否正确?

这是我的网络服务更新功能:

[WebMethod(EnableSession = true)]
public string sell_item(string name, string photo, string description, string initial_price, string bid_time)
{
    SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=Bidding;Integrated Security=True");
    con.Open();
    SqlCommand cmd = new SqlCommand("UPDATE login SET name = @name, photo = @photo, description = @description, initial_price = @initial_price, bid_time = @bid_time where username='"+ Session["username"] +"'", con);
    cmd.Parameters.AddWithValue("@name", name);
    cmd.Parameters.AddWithValue("@photo", photo);
    cmd.Parameters.AddWithValue("@description", description);
    cmd.Parameters.AddWithValue("@initial_price", initial_price);
    cmd.Parameters.AddWithValue("@bid_time", bid_time);
    cmd.ExecuteNonQuery();
    con.Close();
    return "Product has been upload successfully!";
}

这是我调用函数的 Web 应用程序代码:

public partial class bidpage : System.Web.UI.Page
{
abc.Service a = new abc.Service();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = Convert.ToString(a.sell_item(Convert.ToString(TextBoxProduct.Text), Convert.ToString(TextBoxPhoto.Text), Convert.ToString(TextBoxDescription.Text), Convert.ToString(TextBoxPrice.Text), Convert.ToString(TextBoxBidTime.Text)));
}

}

在我的sqlcommand中,我尝试删除WHERE,我的Web应用程序可以将数据更新到数据库,但是对于所有行,更新的数据都相同。但是当我把 WHERE 语句放回 Session["用户名"] 后面时,我在 Web 应用程序中的更新不会存储任何数据。对此有任何帮助吗?我真的不知道如何使用会话更新我的数据。

如何从Web应用程序调用Web服务的更新功能

将 Web 服务添加到服务引用中。

它将生成一个客户端。为服务创建客户端。您可以通过客户端访问该方法。

Yourservice.ServiceClient client = new  Yourservice.ServiceClient();
client.SellItem();

您可以做的是添加一个 try catch,看看是否可以捕获异常并在它有错误时返回一个字符串。