WebService 实例
发布时间:2020-12-17 01:03:15 所属栏目:安全 来源:网络整理
导读:首先新建 “ASP.NET Web 服务应用程序”,之后写个返回string 类型的方法 ,具体实现如下: [WebMethod] public string selectByID(int id) { string name = ""; SqlConnection conn = new SqlConnection ("server=.;database=test;uid=sa;pwd=sa"); DataSet
首先新建 “ASP.NET Web 服务应用程序”,之后写个返回string 类型的方法 ,具体实现如下:
[WebMethod]
public string selectByID(int id)
{
string name = "";
SqlConnection conn = new SqlConnection ("server=.;database=test;uid=sa;pwd=sa");
DataSet ds = new DataSet();
string sql = "select * from aa where id=" + id + "";
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
name = ds.Tables[0].Rows[0]["Name"].ToString();
}
return name;
}
protected void Button1_Click(object sender,EventArgs e)
{
//new 一个webservice 实例
Service.Service1 service = new Service1();
// selectByID 为webservice 方法
string value = service.selectByID(3);
if (value != "")
{
this.Label1.Text = value.ToString();
}
else
{
this.Label1.Text = "Null";
}
}
? 实现效果和数据库截图: ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |