一个通过WebService并通过身份验证来访问远程数据库简单的例子。
首先新建一个WebService文件,内容如下:
<%@WebService Language="C#" Class="UpDateBase"%>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
[WebService(Namespace="http://www.csdn.net")]
public class UpDateBase : WebService
{
[WebMethod]
public DataSet GetDataSet(string UserName,string PassWord)
{
DataSet dataSet=new DataSet();
if(UserName!="" || PassWord!="")
{
SqlConnection Conn=new SqlConnection("server=localhost;database=dgs;uid=sa;pwd=;");
string Sql = "select * from tEmployee where UserName='"+UserName+"' and PassWord='"+PassWord+"'";
SqlDataReader Dr;
SqlCommand Cmd = new SqlCommand(Sql,Conn);
Cmd.Connection.Open();
Dr = Cmd.ExecuteReader();
if(Dr.HasRows==false)
{
Cmd.Connection.Close();
return dataSet;
}
else
{
Cmd.Connection.Close();
string connStr="server=localhost;database=dgs;uid=sa;pwd=;";
SqlConnection conn=new SqlConnection(connStr);
string sqls="select * from WSSnatchSet order by id";
SqlDataAdapter adapter=new SqlDataAdapter();
adapter.SelectCommand=new SqlCommand(sqls,conn);
adapter.Fill(dataSet,"WSSnatchSet");
conn.Close();
return dataSet;
}
}
else
{
return dataSet;
}
}
}
完成后,在MS-DOS状态通过WSDL来生成客户端Cs文件。
命令:wsdl /l:cs /n:DateBase /out:UpDate.cs http://127.0.0.1/WebService.asmx
在客户端新建一个Aspx文件来调用生成的类。
private void Page_Load(object sender,System.EventArgs e)
{
DateBase.UpDateBase UDB = new DateBase.UpDateBase();
DateSet Ds = UDB.GetDataSet("admin","admin").Tables["WSSnatchSet"].DefaultView;
}
这样就可以成功的访问到远程的数据了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。