I have developed WCF service in Visual Studio 2010 and following is the code which worked perfectly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.OracleClient;
namespace OracleEmpService
{
public class Service1 : IService1
{
public List<Employee> getEmployees()
{
List<Employee> employees = new List<Employee>();
string connectionString = "Data Source=orcl;Persist Security Info=True;" +
"User ID=scott;Password=ABC54321;Unicode=True";
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
OracleCommand command = connection.CreateCommand();
string sql;
sql = "SELECT * FROM Emp where rownum<=100000";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Employee employee = new Employee();
employee.Employee_ID = Convert.ToInt32(reader["EmpNo"]);
employee.Employee_Name = Convert.ToString(reader["Ename"]);
employee.Employee_Job = Convert.ToString(reader["Job"]);
employees.Add(employee);
}
return employees.ToList();
}
}
public Employee getEmployeesbyID(Int32 empId)
{
Employee employees = new Employee();
string connectionString = "Data Source=orcl;Persist Security Info=True;" +
"User ID=scott;Password=ABC54321;Unicode=True";
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
OracleCommand command = connection.CreateCommand();
string sql = "SELECT * FROM Emp where EmpNo=" + empId;
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
employees.Employee_ID = empId;
employees.Employee_Name = Convert.ToString(reader["EName"]);
employees.Employee_Job = Convert.ToString(reader["Job"]);
}
}
return employees;
}
}
}
But when I developed ASMX web service and used the following code it is giving error on the name of object "Employee" ..Please guide whats the proper code for this task.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using Microsoft.SharePoint;
using System.Data.OracleClient;
namespace WebServiceDemo
{
public class MyCustomWebService : WebService
{
[WebMethod]
public string getEmployees()
{
List<Employee> employees = new List<Employee>();
string connectionString = "Data Source=orcl;Persist Security Info=True;" + "User ID=scott;Password=ABC54321;Unicode=True";
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
OracleCommand command = connection.CreateCommand();
string sql;
sql = "SELECT * FROM Emp where rownum<=100000";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
if (reader.Read())
{
Employee employee = new Employee();
employee.Employee_ID = Convert.ToInt32(reader["EmpNo"]);
employee.Employee_Name = Convert.ToString(reader["Ename"]);
employee.Employee_Job = Convert.ToString(reader["Job"]);
employees.Add(employee);
return (reader[0].ToString());
}
}
}
}
}