Create WCF service with database

Ratings:
(4)
Views:441
Banner-Img
  • Share this blog:

Example to create WCF service with database

Create a new WCF service with the name example 3 Change the interface name to I emp details 11 change the class name to cls emp details 11 change the SVc file name to cls emp details SVC Write the following code in interface class i.e. I emp details.cs using system data; [service contract] Public interface I emp details { [operation contract] Data table get data(); } Go to emp details.cs file & write the following using system.data; Using system.data; Using system. Data. Sqlclient; Public class cls emp details: I Emp details { Public data table get data() { Sql connection . con = new sql connection (“----“); Sql data adapter da =new sql data adapter (“ select from emp details”, con; Data set Ds = new datsaset(); Da.fill (Ds, “emp details); Return ds. Table [0]; }}  

Creating client component:-

Create a new web with the name check exmaple3 11 design the page add reference to cls empdetails.svc service reference name as “SR1”11 Using SR1; Protected void btn get data_click (--) { Emp Details client Obj1 =new emp de tails client (); Gridview. Data source = obj 1. Get data(); Grid view 1 . dta bind(); } Run the application & check. Screenshot_64  

Example to create a WCF service to perform the operations on database:-

Client design:-

Screenshot_65   Creating WCF service / server component:- Create a new WCF service with the name example 4 Change the interface name from I service.cs to I empdetails.cs 11 change the class name from service.cs to cls emp details.cs 11 change the service file name from service.svc to clsemp details.svc 11 go to the file I emp details.cs & write the following code [service contract] Public interface I empdetails { [operation contract] Int insert rec (emp data obj1); [operation contract] Int insert rec (emp data obj1); [operation contract] Int delete rec (emp data obj1); } [data contract] Public class emp data { Int cls empid, cls deptno; String cls ename, cls designation; Datatime cls Doj, double clssalary ; [data member] Pblic in p emp id { set { cls em pid = value;} [data member] Public string PE name { Set { cls e name = value;} Get { return cls e name:} } [data member] Public string p designation { set { cls designation = value ;} Get { return cls designation;} } [datamember] Public datatime PDOJ { set{ cls Doj = value;} Get{ return clas Doj:} } [data member] Public double psalary { [data member] Public double psalary { set { cls salary = value :} Ge { return clssalary;} } [data member] Public int p dept no { set{ cls deptno = value;} Ge { return cls deptno;} }} Go to cls empdetails.cs class 11 write the following to implement IEmp details interface members using system. Data; Using system.data. sqlclient; Public class cls empdetails :IE mpdetails { sql connection con=new sqlconnection (“---“); Sqlcommand cmd; Public int Insertrec (emp data obj1) { String s = “insert into emp details values (“ PEmpID + “,” + obj1. PE name + obj1/ p designation + “ ‘,’ “ + obj 1 “ ‘, “ + obj1. Psalary + “,” + obj1 +”)” ; Cmd = new sql command (s,con); Con.open(); Int ; = cmd. Execute non query (); Con. Close (); Returen I; } Public int update Rec (emp data obj1) { string s = “ update emp details set Ename = ‘ “ + Pe name + “ ‘, designation = ‘” + obj 1. Gnation + “’, DOJ  = ‘” + obj 1 PDOJ + obj1.pdeptno + “ where empID = “ + objs.pempID; Cmd = new sqlcommand (s,con); Con.open(); Int I = cmd. Execute non query (); Con. Close(); Return I; } Public int delete rec (empdata obj1) { String s = “delete emp details where emp ID = “ + obj1. Pemp id; Cmd = new sqlcommand (s, con); Con.open(); Int I = cmd. Execute nonquery (); Con. Close(); Return I; }}  

Example to create a client to perform the database operations

Create a new website with the name check example 4 Design the webpage 11 add reference to cls empdetails.svc WCF service with reference name as SR1 11 write the following code Using SR1; Empdetails client objs = neco empdetails client(); Emp data objD = new emp data(); Protected void btn instert_click(-----){ objD.PE mp ID = convert. Toint32 (txt emp ID. Text); objd. PeName = txtEName.text; objD.p designation = txt designation.text; objed.PDOJ = convert. To Date time (txtDOJ.Text); objd.psalary = convert. To double (txtsalry. Text); ObjD.pdeptno = convert. TOInt32 (txtdeptno.text); Int I = objs. Insertrec (objD); Lbldisplay. Text = I + “Record (s) Inserted”; Crotected void btnupdate – click (---){ objD.PEm PID = convert . TOInt32 (txtempID.text); objD. PEName = texename. Text; objD.pdesignation= textdesignation.text; objD- PooJ= convert. Todatetime (txtdoj.text); ObjD.psalary = Convet. ToDouble (txtsalry. Text); Objd. Pdeptno = convert. TOIrt32 (txtdeptno. Text); Int I = objs. Updaterec (objD); Laldisplay. Text = I + “record (s) updated”; } protected void btndelete-click(----) { objd.PempID = convert. TOI nt 32 (txt emp ID. Text); Int I = objs. Deleterec (objD); Lbldisplay. Text = I + “record (s) deleted”;  

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox