在我的工程有个App_Code目录
下面有个Service.csusing System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;//引用“System.Data.SqlClient”
using System.Collections;//引用“System.Collections”
using System.Data;//引用“System.Data”[WebService(Namespace = "http://tempuri.org/")]//默认的名称空间
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service ()//构造函数
{
}
public class Employee//定义类
{
public string employeeId;
public string firstName;
public string lastName;
}
[WebMethod]
[System.Xml.Serialization.XmlInclude(typeof(Employee))]//声明“Employee”类可写入XML
public ArrayList GetData()//获得数据库数据
{
SqlConnection conn = new SqlConnection();//定义“SqlConnnection”类实例
//数据库连接字符串
conn.ConnectionString = "server=(local);database=Company;User ID=sa;Pwd=281875";
//定义“SqlCommand”实例,从“Employee”表中取数据
SqlCommand command = new SqlCommand( "select * from Employee",conn);
conn.Open();//打开连接
SqlDataAdapter da = new SqlDataAdapter();//定义“SqlDataAdapter”类实例
da.SelectCommand = command;//将“command”值传递给“SqlDataAdapter”的“SelectCommand”属性
DataSet ds = new DataSet();//定义“DataSet”类实例
da.Fill(ds, "tables");//取数据
ArrayList al = new ArrayList();//定义“ArrayList”类实例
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Employee em = new Employee();//定义“Employee”类实例
//添加数据到“al”变量中
em.employeeId= ds.Tables[0].Rows[i]["employeeId"].ToString().Trim();
em.firstName = ds.Tables[0].Rows[i]["firstName"].ToString().Trim();
em.lastName=ds.Tables[0].Rows[i]["lastName"].ToString().Trim();
al.Add(em);
}
//关闭数据库
conn.Close();
return al;
}
}Service.asmx<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>
我想在Default.aspx.cs文件里使用webserver里面的方法
并输出GetData()里面的数据
该怎么做?
我是很菜的菜鸟,请问大家怎么做?我在这谢谢了!~
下面有个Service.csusing System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;//引用“System.Data.SqlClient”
using System.Collections;//引用“System.Collections”
using System.Data;//引用“System.Data”[WebService(Namespace = "http://tempuri.org/")]//默认的名称空间
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service ()//构造函数
{
}
public class Employee//定义类
{
public string employeeId;
public string firstName;
public string lastName;
}
[WebMethod]
[System.Xml.Serialization.XmlInclude(typeof(Employee))]//声明“Employee”类可写入XML
public ArrayList GetData()//获得数据库数据
{
SqlConnection conn = new SqlConnection();//定义“SqlConnnection”类实例
//数据库连接字符串
conn.ConnectionString = "server=(local);database=Company;User ID=sa;Pwd=281875";
//定义“SqlCommand”实例,从“Employee”表中取数据
SqlCommand command = new SqlCommand( "select * from Employee",conn);
conn.Open();//打开连接
SqlDataAdapter da = new SqlDataAdapter();//定义“SqlDataAdapter”类实例
da.SelectCommand = command;//将“command”值传递给“SqlDataAdapter”的“SelectCommand”属性
DataSet ds = new DataSet();//定义“DataSet”类实例
da.Fill(ds, "tables");//取数据
ArrayList al = new ArrayList();//定义“ArrayList”类实例
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Employee em = new Employee();//定义“Employee”类实例
//添加数据到“al”变量中
em.employeeId= ds.Tables[0].Rows[i]["employeeId"].ToString().Trim();
em.firstName = ds.Tables[0].Rows[i]["firstName"].ToString().Trim();
em.lastName=ds.Tables[0].Rows[i]["lastName"].ToString().Trim();
al.Add(em);
}
//关闭数据库
conn.Close();
return al;
}
}Service.asmx<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>
我想在Default.aspx.cs文件里使用webserver里面的方法
并输出GetData()里面的数据
该怎么做?
我是很菜的菜鸟,请问大家怎么做?我在这谢谢了!~
解决方案 »
- 客户端 return false 页面还刷新
- 重新布置环境后,ASP.NET运行不了。HTTP status 404: Not Found. 大家帮我看看。
- girdview 在网页中定位问题
- 附件下载 代码 在线等~~~~~~~~~~~
- 急急急急,Response.OutputStream.Write()能输出Swf格式的文件吗?---在线等,急
- 获取上传文件的文件名????
- 还是 1000 分,第三次问同一问题,(关于使用想对路径生成文件夹,万网不给我权限,请问有没有别的写法)
- Aspx已经设置Body的topmargin="0"了,为什么页面最上面还有大约3个象素那么高的空白啊?
- CSS+Div写的 也面布局 但是本机 正常,到服务器上 CSS就不起作用了,(网落是慢点) 不知道与这有关系吗?
- 绝对高度!谁知道如何判断一个按纽是否执行!
- Asp.net 3.5 Ajax上的普遍问题,无人能解!!!(在线等待)
- 有劳大家推荐几本书???
<%= new Service().GetData()%>
localhost.Service1 ser=new News.localhost.Service1();
string a=ser.HelloWorld();
我会了
但是怎么输出GetData()里面的数?