加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

2.自己写WebService提供Add和getStudent服务。

发布时间:2020-12-17 00:28:13 所属栏目:安全 来源:网络整理
导读:2.自己写WebService提供Add和getStudent服务。 ? WebService1.asmx.cs ?public class WebService1 : System.Web.Services.WebService ?{ ? [WebMethod] ?public int Add(int a,int b) ?{ ?return a + b; ?} ?[WebMethod] ?public DataSet GetStudent() ?{ ?s
2.自己写WebService提供Add和getStudent服务。
?
WebService1.asmx.cs
?public class WebService1 : System.Web.Services.WebService
?{
?
[WebMethod]
?public int Add(int a,int b)
?{
?return a + b;
?}
?[WebMethod]
?public DataSet GetStudent()
?{
?string constr = ConfigurationManager.ConnectionStrings["studentConstr"].ConnectionString;
?string sql = "select * from student";
?SqlDataAdapter adapter = new SqlDataAdapter(sql,constr);
?DataSet ds = new DataSet();
?adapter.Fill(ds);
?return ds;
?}
?}
Web.config
<configuration>
?<system.web>
?<webServices>
?<protocols>
?<add name="HttpGet"/>
?</protocols>
?</webServices>
?<compilation debug="true" targetFramework="4.0" />
?</system.web>
?<connectionStrings>
?<add name="studentConstr" connectionString="data source=.;initial catalog=student;user

id=sa;password=222221;"/>
?</connectionStrings>
</configuration>
?


WebAddStudent.aspx前台
<body>
?<form id="form1" runat="server">
?<div>
?<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;+
?<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
&nbsp;<asp:Button ID="Button1" runat="server" Xonclick="Button1_Click" Text="Button" />
&nbsp;<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
?
<br />
?<br />
?<asp:GridView ID="GridView1" runat="server">
?</asp:GridView>
?
</div>
?</form>
</body>
?
WebAddStudent.aspx.cs后台
?
protected void Button1_Click(object sender,EventArgs e)
?{
?localhost.WebService1 ws=new localhost.WebService1();
?this.TextBox3.Text= ws.Add(Convert.ToInt32(this.TextBox1.Text),Convert.ToInt32
?
(this.TextBox2.Text)).ToString();
?
this.GridView1.DataSource = ws.GetStudent();
?this.GridView1.DataBind();
?}
?

Web.config
<configuration>
?<configSections>
?<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup,

System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" >
?<section name="userWebService.Properties.Settings"

type="System.Configuration.ClientSettingsSection,System,

PublicKeyToken=b77a5c561934e089" requirePermission="false" /> ?</sectionGroup> ?</configSections> ?<system.web> ?<compilation debug="true" targetFramework="4.0" /> ?</system.web> ? <applicationSettings> ?<userWebService.Properties.Settings> ?<setting name="userWebService_localhost_WebService1" serializeAs="String"> ?<value>http://localhost:1095/WebService1.asmx</value> ?</setting> ?</userWebService.Properties.Settings> ?</applicationSettings> </configuration> ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读