用WebService实现对数据库进行操作(添加+删除+修改)

yang669933 2017-05-21 11:45:59

[WebMethod(Description="添加")]
public int Add(string id,string name,string title,string depar,string ass)
{
SqlConnection conn = new SqlConnection("hisdbConnectionString");
conn.Open();
string sql = "intsert into Doctor(dID,dName,Title,Department,Assistment) values(@dID,@dName,@Title,@Department,@Assistment)";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter d = new SqlParameter("@dID", id);
cmd.Parameters.Add(d);
SqlParameter n= new SqlParameter("@dName", name);
cmd.Parameters.Add(n);
SqlParameter t = new SqlParameter("@Title", title);
cmd.Parameters.Add(t);
SqlParameter e = new SqlParameter("@Department", depar);
cmd.Parameters.Add(e);
SqlParameter a = new SqlParameter("@Assistment", ass);
cmd.Parameters.Add(a);
int result = cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
return result;
}
[WebMethod(Description = "更新")]
public int upda(string id, string name, string title, string depar, string ass)
{
SqlConnection conn = new SqlConnection("hisdbConnectionString");
conn.Open();
string sql = "update Doctor set dName=@dName,Title=@Title,Department=@Department,Assistment=@Assistment where dID=@dID";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter d = new SqlParameter("@dID", id);
cmd.Parameters.Add(d);
SqlParameter n = new SqlParameter("@dName", name);
cmd.Parameters.Add(n);
SqlParameter t = new SqlParameter("@Title", title);
cmd.Parameters.Add(t);
SqlParameter e = new SqlParameter("@Department", depar);
cmd.Parameters.Add(e);
SqlParameter a = new SqlParameter("@Assistment", ass);
cmd.Parameters.Add(a);
int result = cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
return result;
}
[WebMethod(Description = "删除")]
public int del(string id)
{
SqlConnection conn = new SqlConnection("hisdbConnectionString");
conn.Open();
string sql = "delete * from Doctot where dID=@dID";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter d = new SqlParameter("@dID", id);
cmd.Parameters.Add(d);
int result = cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
return result;
}

创建了一个web service不知道如何调用这些方法。
<body>
<form id="form1" runat="server">
<div style="height: 433px; width: 670px">

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
编号:<asp:TextBox ID="txt_1" runat="server"></asp:TextBox>
<br />
姓名:<asp:TextBox ID="txt_2" runat="server" ></asp:TextBox>
<br />
职称:<asp:TextBox ID="txt_3" runat="server" ></asp:TextBox>
<br />

科室:<asp:TextBox ID="txt_4" runat="server" ></asp:TextBox>
<br />
科室:<asp:TextBox ID="txt_5" runat="server" ></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="添加" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="更新" />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="删除" />


</div>
</form>
</body>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class _112 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connstr = ConfigurationManager.ConnectionStrings["hisdbConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connstr);
try
{
conn.Open();
SqlDataAdapter ad = new SqlDataAdapter("Select * from Doctor", conn);
DataSet ds = new DataSet();
ad.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
catch { }
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//SELECT [pID], [pName], [pSex], [pDep], [pClass] FROM [pPerson]
int selectIndex = this.GridView1.SelectedIndex;
txt_1.Text = this.GridView1.DataKeys[selectIndex]["dID"].ToString();
txt_1.Enabled = false;
GridViewRow gvrow = this.GridView1.SelectedRow;
txt_2.Text = gvrow.Cells[2].Text.ToString();
txt_3.Text = gvrow.Cells[3].Text.ToString();
txt_4.Text = gvrow.Cells[4].Text.ToString();
txt_5.Text = gvrow.Cells[5].Text.ToString();



}

protected void Button1_Click(object sender, EventArgs e)
{

localhost.WebService1 web=new localhost.WebService1();
web.Add(txt_1.Text, txt_2.Text, txt_3.Text, txt_4.Text, txt_5.Text);

}
protected void Button2_Click(object sender, EventArgs e)
{
localhost.WebService1 web = new localhost.WebService1();
web.upda(txt_1.Text, txt_2.Text, txt_3.Text, txt_4.Text, txt_5.Text);
}
protected void Button3_Click(object sender, EventArgs e)
{
localhost.WebService1 web = new localhost.WebService1();
web.del(txt_1.Text);
}


}


点击按钮不能实现添加+删除+修改,请大神看看
...全文
425 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
珠穆郎码疯@ 2019-06-19
  • 打赏
  • 举报
回复
您这是什么语言写的啊?
  • 打赏
  • 举报
回复
在你的 public int Add(string id,string name,string title,string depar,string ass) 方法的第一行设置断点,然后开始调试,单步跟踪,在各种调试窗口分析变量值...... 学编程只会抄代码而不知道原理、不知如何调试,其实是等于没学,那样你除了只“求结果”以外连描述你遇到的问题都不会。
yang669933 2017-05-21
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
在你的 public int Add(string id,string name,string title,string depar,string ass) 方法的第一行设置断点,然后开始调试,单步跟踪,在各种调试窗口分析变量值...... 学编程只会抄代码而不知道原理、不知如何调试,其实是等于没学,那样你除了只“求结果”以外连描述你遇到的问题都不会。
谢谢,我尝试了,也找到问题了
项目Maven构建,真实大型互联网架构,做到高并发,大数据处理,整个项目使用定制化服务思想,提供模块化、服务化、原子化的方案,将功能模块进行拆分,可以公用到所有的项目中。架构采用分布式部署架构,所有模块进行拆分,使项目做到绝对解耦,稳定压倒一切~~ 持续集成: 1. 我的待办工作流服务(提供Webservice服务) 2. 我的待办工作流集成JMS消息服务(支持高并发,可支持成千上万系统集成) 3. 我的任务提供Rest服务,完成日常的工作管理,通过定时调度平台,动态生成我的任务、循环周期任务、定时邮催提醒完成任务等 4. 文件上传、多线程下载服务化、发送邮件、短信服务化、部门信息服务化、产品信息服务化、信息发布服务化、我的订阅服务化、我的任务服务化、公共链接、我的收藏服务化等 系统模块: 1. 用户管理: 用户信息管理(添加删除修改、用户授权、用户栏目管理、查询等) 用户组管理(添加删除修改、用户组栏目授权,栏目授权、查询、用户组人员添加查询等) 用户角色管理(添加删除修改、用户角色授权、用户角色栏目信息查询设置等) 2. 文章管理: 栏目管理:查询无限极栏目树、创建无限极栏目树分类(导航栏目、图片列表栏目、文章列表栏目、文章内容栏目等)、删除修改栏目信息。 文章管理:创建、删除修改文章,多维度文章查询,包括已发布、未发布、所有文章等。文章富文本编辑器、文章多文件上传、文章状态控制等。 3. 系统设置: 数据字典管理:支持中、英文信息,支持无限级别分类配置,动态控制是否可用等。 部门信息管理:支持中、英文无限级别部门信息增加,删除修改操作,部门列表、树心查询等。 日志管理:系统日志列表查询、在线查看、在线下载等 路线管理:集成百度地图API,提供线路查询管理功能 Druid Monitor(监控):集成阿里巴巴连接池,提供在线连接池监控程序,包括:数据源、SQL监控、URL监控、Session监控、Spring监控等 网站信息管理:通过系统配置文件进行网站内容操作,包括邮件服务器配置、公司基本信息配置等。 4. 集成REST服务,可以用作独立服务平台(提供大量实例及测试平台,包括:文件上传下载、邮件短信发送、部门、产品、公共连接、我的收藏、我的任务、信息发布等) 5. 集成Quartz调度,可以用作定时调度平台(动态配置调度类、调度时间,使程序自动执行某些业务) 6. Lucene搜索引擎,可以将文件资料索引化,支持文件内容搜索、关键字搜索、高亮关键字等,使信息在毫秒内提取查询出来 7. 用户设置功能:包括修改用户信息,修改密码、发送消息,修改个人图片,查看角色、查看用户组,管理员修改角色、用户、用户组等。 8. 集成Webservice平台,包括jaxws服务、CXF框架,配置双加密的权限认证。使服务集成更加安全。 9. Bootstrap html5提供了两套前台开环境,包括CMS和电子商务网站,使您的开发更加的简洁。 技术点: 1. Springmvc + Mybatis集成、SpringSecurity权限控制、Spring AOP事务处理。 2. Wink Rest服务、Webservice服务:jaxws、CXF等 3. IO 流上传下载文件,多线程操作 4. 发送邮件,配置邮件服务器,发基于html、纯文本格式的邮件(可以免费赠送网络爬虫,使其群发邮件,做到广告推送等) 5. MD5加密(登陆密码校验加密等),用户统一Session、Cookie管理,统一验证码校验等。 6. 数据库连接池统一配置 7. Quartz定时调度任务集成(直接通过配置即可) 8. Httpclient破解验证码,登陆联通充值平台 9. 汉字、英文拆分、可以用作文档关键字搜索等。 10. Base64图片处理,支持PC,Android,IOS 11. Service Socket 、Client Socket 通信技术(已经做过GPRS数据获取,并用到了项目中) 12. 提供大量工具类,可以直接使用 13. Maven项目构建,您可以直接做架构,可以提升自己的学习能力,使您成为真正的架构师。 版本支持: 支持版本: jdk 1.6、1.7、1.8 Web容器: Tomcat 6、7、 8 数据库: mysql

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧