打开vs.net, 新建一个visual c#的ASP.net Web服务项目, 出来的界面切换到代码页, 写公共函数

解决方案 »

  1.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    using System.Configuration;
    using System.Data.SqlClient;namespace WebSvr
    { public class UserC : System.Web.Services.WebService
    {
    public UserC()
    {
    //CODEGEN:该调用是 ASP.NET Web 服务设计器所必需的
    InitializeComponent();
    } #region Component Designer generated code

    //Web 服务设计器所必需的
    private IContainer components = null;

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    { } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if(disposing && components != null)
    {
    components.Dispose();
    }
    base.Dispose(disposing);
    }

    #endregion //设置数据库连接
    string strConn=ConfigurationSettings.AppSettings["StrConn"];

    //检查用户登陆
    [WebMethod]
    public bool ChkLogin(string username,string password)
    {
    SqlConnection conn=new SqlConnection(strConn);
    conn.Open();
    string strSql="select * from userinfo where username='"+username+"' and password='"+password+"'";
    SqlCommand cmd=new SqlCommand(strSql,conn);
    SqlDataReader dr;
    dr=cmd.ExecuteReader();
    bool isRead=dr.Read();
    conn.Close();
    return isRead;
    } [WebMethod]
    public DataSet ShowTopic(string typeName,int topicNum)
    {
    SqlConnection conn=new SqlConnection(strConn);
    conn.Open();
    string strSql="select top "+topicNum +" title,id,pubtime from "+ typeName +" order by id desc";
    SqlDataAdapter da=new SqlDataAdapter(strSql,conn);
    DataSet ds=new DataSet();
    da.Fill(ds,typeName);

    conn.Close();
    return ds; } [WebMethod]
    public DataSet ShowUserList()
    {
    SqlConnection conn=new SqlConnection(strConn);
    conn.Open();
    string strSql="select top 30 * from userinfo order by id desc";
    SqlDataAdapter da=new SqlDataAdapter(strSql,conn);
    DataSet ds=new DataSet();
    da.Fill(ds,"UserList");

    conn.Close();
    return ds; } [WebMethod]
    public bool UserReg(string username,string password,string msn,string email,string sex,string birthday,string homepage,string mphone) {
    try {
    SqlConnection conn=new SqlConnection(strConn);
    conn.Open();
    string strSqlChk="select * from userinfo where username='"+username+"'";
    SqlCommand cmdChk=new SqlCommand(strSqlChk,conn);
    SqlDataReader dr;
    dr=cmdChk.ExecuteReader();
    if(dr.Read())
    {
    conn.Close();
    return false;

    }
    else
    {
    conn.Close();
    string strSql="insert into userinfo(username,password,sex,birthday,homepage,mphone,email,msn)"+ 
    " values('"+username+"','"+password+"','"+sex+"','"+birthday+"','"+homepage+"','"+mphone+"','"+email+"','"+msn+"')";
    SqlCommand cmd=new SqlCommand(strSql,conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    return true;
    }
    }
    catch
    {
    return false;
    }
    }
    [WebMethod]
    public DataSet ShowUserInfo(string username)
    {
    SqlConnection conn=new SqlConnection(strConn);
    conn.Open();
    string strSql="select  * from userinfo where username='"+username+"'";
    SqlDataAdapter da=new SqlDataAdapter(strSql,conn);
    DataSet ds=new DataSet();
    da.Fill(ds,username);

    conn.Close();
    return ds; }
    [WebMethod]
    public bool UserRegModify(string username,string password,string msn,string email,string sex,string birthday,string homepage,string mphone) 
    {
    try 
    {
    SqlConnection conn=new SqlConnection(strConn);
    //conn.Open();
    string strSql="update userinfo set password='"+password+"',sex='"+sex+"',birthday='"+birthday+"',homepage='"+homepage+"',mphone='"+mphone+"',email='"+email+"',msn='"+msn+"' where username='"+username+"'"; 
    SqlCommand cmd=new SqlCommand(strSql,conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    return true;

    }
    catch
    {
    return false;
    }
    }


    }
    }
      

  2.   

    1 上为建 WebServiece2 添加WEB引用 输入地址 Http://localhost/目录名/WEBSERVICE名.asmx3 创建实例 WS.UserC myWs=new WS.UserC();4 使用方法dgridAnnounce.DataSource=myWs.ShowTopic("new",10);
    dgridAnnounce.DataBind();
      

  3.   

    To yeefly(Topindotnet);在服务上你定义是的:namespace WebSvr
    怎么建立实例的时候用:WS.UserC myWs=new WS.UserC();
    可是我看其他的是用:localhost.Service Myws=new localhost.Service();
    能具体些吗?
      

  4.   

    mathservice.cs  // 需要编译成为.DLL文件,放在BIN的文件夹里
    //-------------------------------------------------------------------------
    using System;
    using System.Web.Services;namespace MathService
    {
    [WebService(Namespace="http://www.contoso.com/")]
    public class MathService:WebService

      [WebMethod]
      public float add(float a,float b)
      {
         return a+b;  }
      [WebMethod]
      public float Subtract(float a,float b)
      {
         return a-b;
      }
      [WebMethod]
      public float Multiply(float a,float b)
      {
         return a*b;
      }
      [WebMethod]
      public float Divide(float a,float b)
      {
         if(b==0) return -1;
         return a/b;
      }
    }
    }//---------MATHSERVERCLIENT.ASPX------------------------------------
    <%@ import Namespace="MathService" %>
    <html>
    <script language="c#" runat="server">
    float xx=0;
    float yy=0;public void Submit_Click(Object s,EventArgs e)
    {
      try
       {
         xx=float.Parse(x.Text);
         yy=float.Parse(y.Text);
        }   catch(Exception){}  MathService service=new MathService();  switch(((Control)s).ID)
       {
         case"加":Result.Text="<b>结果<b>=" + service.add(xx,yy).ToString();break;
         case"减":Result.Text="<b>结果<b>=" + service.Subtract(xx,yy).ToString();break;
         case"乘":Result.Text="<b>结果<b>=" + service.Multiply(xx,yy).ToString();break;
         case"除":Result.Text="<b>结果<b>=" + service.Divide(xx,yy).ToString();break;  
      }
    }
    </script>
    <body style="font:10.5pt 宋体">
    <center>
    <h4>使用简单的数学服务</h4>
    <form runat=server>
    <div style="padding:15,15,15,15;background-color:#cccccc;width=170;border-color:black;border-width=1;border-style:solid">操作数1:<br><asp:TextBox id="x" Text="15" runat="server" />
    操作数2:<br><asp:TextBox id="y" Text="5" runat="server" /><input type="submit" id="加" value="加" Onserverclick="Submit_Click" runat="server">
    <input type="submit" id="减" value="减" Onserverclick="Submit_Click" runat="server">
    <input type="submit" id="乘" value="乘" Onserverclick="Submit_Click" runat="server">
    <input type="submit" id="除" value="除" Onserverclick="Submit_Click" runat="server">
    <p>
    <asp:label id="Result" runat=server />
    </div>
    </form>
    </center>
    </body>
    </html>