Getting Data from SQL server by using WCF


IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCF_DataLayer
{
[ServiceContract]
public interface IService1
{
[OperationContract]
List GetWCFdata();
[OperationContract]
void SubmitData(WCF_Data myData);
[OperationContract]
List GetDataFromServer();
[OperationContract]
void SetWCFdata(WCF_Data myData);
}

[DataContract]
public class WCF_Data
{
[DataMember]
public string uname
{
get;
set;
}
[DataMember]
public string address
{
get;
set;
}
[DataMember]
public int phone
{
get;
set;
}
}
}

Service1.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace WCF_DataLayer
{
public class Service1 : IService1
{
List DataToSave = new List();

public List GetWCFdata()
{
List myData = new List();
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader sdr = null;
try
{
conn = new SqlConnection(@"server=zayyaroo\shop1;database=test;
uid=sa;pwd=123;");
conn.Open();
cmd = new SqlCommand("SELECT * FROM WCF_data", conn);
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
WCF_Data data = new WCF_Data();
data.uname = sdr["uname"].ToString();
data.address = sdr["address"].ToString();
data.phone = int.Parse(sdr["phone"].ToString());
myData.Add(data);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sdr != null)
sdr.Dispose();
if (conn != null)
conn.Dispose();
if (cmd != null)
cmd.Dispose();
}
return myData;
conn.Close();
}
public void SubmitData(WCF_Data myData)
{
DataToSave.Add(myData);
}
public List GetDataFromServer()
{
return DataToSave;
}
public void SetWCFdata(WCF_Data myData)
{
SqlConnection connection = new SqlConnection(@"server=zayyaroo\shop1;
database=test;uid=sa;pwd=123;");
connection.Close();
connection.Open();
SqlTransaction tran = connection.BeginTransaction();
SqlCommand command = new SqlCommand("INSERT INTO [WCF_data](uname,address,phone)"
+ "VALUES(@uname,@addr,@phone)", connection, tran);
try
{
command.Parameters.Add("@uname", SqlDbType.NVarChar, 50).Value = myData.uname.ToString();
command.Parameters.Add("@addr", SqlDbType.NVarChar, 50).Value = myData.address.ToString();
command.Parameters.Add("@phone", SqlDbType.Int).Value = int.Parse(myData.phone.ToString());
command.ExecuteNonQuery();
tran.Commit();
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
}
}


Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForm
{
public partial class Form1 : Form
{
ServiceReference1.Service1Client svcClient = null;
ServiceReference1.WCF_Data oneData = new
WinForm.ServiceReference1.WCF_Data();

public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
svcClient = new ServiceReference1.Service1Client();
svcClient.Open();
}
private void btnAdd_Click(object sender, EventArgs e)
{
oneData.uname = tbUname.Text;
oneData.address = tbAddr.Text;
oneData.phone = int.Parse(tbPhone.Text);
svcClient.SubmitData(oneData);
svcClient.SetWCFdata(oneData);
}
private void btnSend_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = svcClient.GetDataFromServer();
dataGridView2.DataSource = svcClient.GetWCFdata();
}
private void Form_closing(object sender, FormClosingEventArgs e)
{
svcClient.Close();
svcClient = null;
}
}
}


Idea come from .. http://www.a2zdotnet.com/View.aspx?Id=110

No Response to "Getting Data from SQL server by using WCF"

Post a Comment

powered by Blogger