Asp.Net从零开始学-22

来源:互联网 发布:新媒体营销 数据分析 编辑:程序博客网 时间:2024/05/18 02:23
简单提一下WebService 
[WebMethod]
  public string HelloWorld(string name)
  {
   return "Hello+name";
  }

<form name="f1" method="post" action="http://localhost/WebServiceTest/Service1.asmx/HelloWorld">
 <input type="text" name="name"><input type="submit">     <!--name="name"通过此传-->
</form>

[WebMethod(Description="获取库存")]
  public DataSet getStore(){
   SqlConnection con=new SqlConnection("server=.;database=store;uid=sa;pwd=12345678");
   SqlDataAdapter sda=new SqlDataAdapter();
   sda.SelectCommand=new SqlCommand("select * from store",con);
   DataSet ds=new DataSet();
   sda.Fill(ds,"store");
   return ds;
  }

private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   WebServiceTest.Service1 ss=new WebServiceTest.Service1();
   this.DataGrid1.DataSource=ss.getStore();
   this.DataGrid1.DataBind();
  }

[WebMethod]
  public string AtoB(int sum){
   try{
    this.decrease(sum);
    this.increase(sum);
    return "成功!";
   }
   catch{
    return "失败!";
   }
  }
  private void decrease(int sum){
   SqlConnection con=new SqlConnection("server=.;database=store;uid=sa;pwd=12345678");
   con.Open();
   SqlCommand cmd=new SqlCommand("update aaa set money=money-"+sum.ToString()+"where account='A'",con);
   cmd.ExecuteNonQuery();
   con.Close();
  }
  private void increase(int sum)
  {
   SqlConnection con=new SqlConnection("server=.;database=store;uid=sa;pwd=12345678");
   con.Open();
   SqlCommand cmd=new SqlCommand("update aaa set money=money+"+sum.ToString()+"where account='B'",con);
   cmd.ExecuteNonQuery();
   con.Close();
  }
验证a、b双方转帐