我正在实现服务通信服务,即IIS wcf服务和Windows服务中托管的wcf
创建Iservice1接口并声明两个方法 [服务合约] 公共接口IService1 {
[OperationContract]
string InsertUserDetails(UserDetails userInfo);
[OperationContract]
DataSet SelectUserDetails();
}
在Service1.svc类
中实现此方法公共类Service1:IService1 { // string connetionString = null; OleDbConnection cnn; string connetionString = @“Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\ Anand \ Database \ UserDB.accdb”;
public DataSet SelectUserDetails()
{
OleDbConnection cnn;
cnn = new OleDbConnection(connetionString);
cnn.Open();
OleDbCommand cmd = new OleDbCommand("select UserID,UserName,Country,Email from
UserRecord order by UserId ", cnn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.ExecuteNonQuery();
cnn.Close();
return ds;
}
public string InsertUserDetails(UserDetails userInfo)
{
string Message;
OleDbConnection cnn;
cnn = new OleDbConnection(connetionString);
cnn.Open();
string str = "insert into UserRecord values('"+userInfo.UserID+"','" +
userInfo.UserName + "','" + userInfo.Password + "','" + userInfo.Country +
"','" + userInfo.Email + "') ";
OleDbCommand cmd = new OleDbCommand(str, cnn);
try
{
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Message = userInfo.UserName + " Details inserted successfully";
}
else
{
Message = userInfo.UserName + " Details not inserted successfully";
}
cnn.Close();
return Message;
}
catch (Exception)
{
throw;
}
}
之后创建Windows服务并将服务引用提供给Windows服务并创建服务引用对象
public partial class UserRegistration : Form
{ ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
public UserRegistration()
{
InitializeComponent();
showdata();
}
private void showdata() // to show the data in the DataGridView
{
DataSet ds = new DataSet();
ds = service.SelectUserDetails();
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.UserDetails objuserdetail = new ServiceReference1.UserDetails(); // add type reference
objuserdetail.UserID = Int32.Parse(txtuserId.Text);
objuserdetail.UserName = txtUserName.Text;
objuserdetail.Password = txtPass.Text;
objuserdetail.Country = txtCountry.Text;
objuserdetail.Email = txtEmailId.Text;
service.InsertUserDetails(objuserdetail);
showdata();
Empty();
}
public void Empty()
{
txtCountry.Text = "";
txtEmailId.Text = "";
txtPass.Text = "";
txtuserId.Text = "";
txtUserName.Text = "";
}
}