Ejemplo n.º 1
0
        public void TestAllEmployees()
        {
            List <EmpDetails> listEmpDetails = new List <EmpDetails>();

            string        cs  = System.Configuration.ConfigurationManager.ConnectionStrings["servicestring"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);
            //string query1 = ("select e.Emp_ID, e.Emp_Name, e.Salary, a.Address1, a.Address2, a.city, s.Hourly_wages, s.Payment_Date from Employee as e inner join Address as a on e.Emp_ID = a.Emp_ID inner join Salary as s on e.Emp_ID = s.Emp_id; ");

            SqlCommand cmd = new SqlCommand();

            cmd.Connection  = con;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "WebserviceDB";
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                Employee   employee = new Employee();
                Address    addr     = new Address();
                Salary     sal      = new Salary();
                EmpDetails empd     = new EmpDetails();
                employee.emp_id   = Convert.ToInt32(rdr["Emp_ID"]);
                employee.emp_name = rdr["Emp_Name"].ToString();
                employee.salary   = Convert.ToInt32(rdr["Salary"]);
                addr.address1     = rdr["Address1"].ToString();
                addr.address2     = rdr["Address2"].ToString();
                addr.city         = rdr["city"].ToString();
                sal.hourly_wages  = Convert.ToInt32(rdr["Hourly_wages"]);
                sal.payment_Date  = rdr["Payment_Date"].ToString();
                empd.employee     = employee;
                empd.addr         = addr;
                empd.sal          = sal;

                listEmpDetails.Add(empd);
            }
            JavaScriptSerializer js = new JavaScriptSerializer();

            Context.Response.Write(js.Serialize(listEmpDetails));

            //return JsonConvert.SerializeObject(listEmpDetails, Newtonsoft.Json.Formatting.Indented);
        }
Ejemplo n.º 2
0
        public void Parsedata(string json)
        {
            //create an instance of HttpClient
            HttpClient client = new HttpClient();

            List <EmpDetails> listEmpDetails = new List <EmpDetails>();

            //DefaultRequestHeader to Json
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Create an instance of HttpResponse & invoke the service asynchronously
            //HttpResponseMessage response = client.GetAsync("http://*****:*****@"INSERT INTO Employee(emp_id, emp_name, salary) VALUES(@emp_id, @emp_name, @salary);
            INSERT INTO Address(address1, address2, city) VALUES(@address1, @address2, @city);
            INSERT INTO Salary(hourly_wages, payment_Date) VALUES(@hourly_wages, @payment_Date)";

            cmd.Parameters.Add("@emp_id", SqlDbType.VarChar).Value   = emp_id;
            cmd.Parameters.Add("@emp_name", SqlDbType.VarChar).Value = emp_name;
            cmd.Parameters.Add("@salary", SqlDbType.VarChar).Value   = salary;

            cmd.Parameters.Add("@address1", SqlDbType.VarChar).Value = address1;
            cmd.Parameters.Add("@address2", SqlDbType.VarChar).Value = address2;
            cmd.Parameters.Add("@city", SqlDbType.VarChar).Value     = city;

            cmd.Parameters.Add("@hourly_wages", SqlDbType.VarChar).Value = hourly_wages;
            cmd.Parameters.Add("@payment_Date", SqlDbType.VarChar).Value = payment_Date;

            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();


            //String Name = "abc";

            //String Address = "abc address";
            //String Company = "xyz company";
            //String Address2 = "xyz company address";

            //SqlCommand sqlcmd = new SqlCommand();
            //SqlConnection cn = new SqlConnection();
            //cn.ConnectionString = ConfigurationManager.ConnectionStrings["LOC_MASTERConnectionString"].ToString();
            //sqlcmd.CommandType = CommandType.Text;
            //sqlcmd.Connection = cn;

            //sqlcmd.CommandText = @"INSERT INTO Employee(emp_id, emp_name, salary) VALUES(@emp_id, @emp_name, @salary);
            //INSERT INTO Table2(Company, Address2) VALUES(@Company, @Address2)";

            //sqlcmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
            //sqlcmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = Address;

            //sqlcmd.Parameters.Add("@Company", SqlDbType.VarChar).Value = Company;
            //sqlcmd.Parameters.Add("@Address2", SqlDbType.VarChar).Value = Address2;

            //if (cn.State == ConnectionState.Open) { cn.Close(); }
            //cn.Open();
            //sqlcmd.ExecuteNonQuery();
            //cn.Close();
        }