打开vs.net, 新建一个visual c#的ASP.net Web服务项目, 出来的界面切换到代码页, 写公共函数
解决方案 »
- 分享帖:使用Excel COM组件导出数据后释放Excel资源
- AJAX的问题,刷新一下又没事了,请问如何解?(有图)
- 各位大侠,我第一次使用水晶报表作报表,能不能提供连接数据库的例子
- 我在游标里面要用rollback怎么写呢?
- ****************请问关于js的问题,有点挑战,高手请进**********************
- 对接口的一点疑问,请各位高手帮忙解决啊
- 连接mysql数据库的问题,求帮忙,在线等
- c# 用中文作为hashtable的键有没有什么问题
- 使用jmail怎么删除邮件和更改密码
- 怎么通过后台自动增长数
- 求助 javascript 出错,急急请各位帮忙!!
- 高手的来,怎么屏蔽这个字: 这个是用智能abc按v1输入的
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;
}
}
}
}
dgridAnnounce.DataBind();
怎么建立实例的时候用:WS.UserC myWs=new WS.UserC();
可是我看其他的是用:localhost.Service Myws=new localhost.Service();
能具体些吗?
//-------------------------------------------------------------------------
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>