使用WCF在Windows窗体中进行CRUD操作 [英] CRUD operation in windows form using WCF

查看:75
本文介绍了使用WCF在Windows窗体中进行CRUD操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一条编译错误消息,在Service1.cs中并非所有代码路径都返回InsertCustomerDetails中的值,所有代码都是正确的。我该如何纠正呢。请建议。

这是一个代码: -



Service1.cs: -



公共类服务1:IService1

{

公共字符串InsertCustomerDetails(CustomerDetails custinfo)

{

SqlConnection con = new SqlConnection(数据源:ANKUSH \\ SQLEXPRESS;初始目录= ankush);

con.Open();

SqlCommand cmd = new SqlCommand (插入客户(Id,First_Name,Last_Name,Age)值(@ id,@ firstname,@ lastname,@ age),con);

cmd.Parameters.AddWithValue(@ id ,custinfo.Id);

cmd.Parameters.AddWithValue(@ firstname,custinfo.Firstname);

cmd.Parameters.AddWithValue(@ lastname, custinfo.Lastname);

cmd.Parameters.AddWithValue(@ age,custinfo.Age);

int i = cmd.ExecuteNonQuery();

con.Close();

}

}



IService1.cs: -

[ServiceContract]

公共接口IService1

{

[OperationContract]

string InsertCustomerDetails(CustomerDetails userInfo);

}



[DataContract]

公共类CustomerDetails

{

int id;



[DataMember]

public int Id

{

得到{return id; }

set {id = value; }

}

字符串名字;



[DataMember]

公共字符串名字

{

得到{返回名字; }

set {firstname = value; }

}

string lastname;



[DataMember]

public string姓氏

{

get {return lastname; }

set {lastname = value; }

}

int age;



[DataMember]

public int年龄

{

获得{返回年龄; }

set {age = value; }

}

}

I have getting an compilation error message that in Service1.cs- "not all code paths return a value" in "InsertCustomerDetails", all the codes are correct. How should i correct it. Please sugget.
Here's a code:-

Service1.cs:-

public class Service1 : IService1
{
public string InsertCustomerDetails(CustomerDetails custinfo)
{
SqlConnection con = new SqlConnection("data source:ANKUSH\\SQLEXPRESS; initial catalog=ankush");
con.Open();
SqlCommand cmd = new SqlCommand("insert into customer (Id, First_Name, Last_Name, Age) values(@id, @firstname, @lastname, @age)", con);
cmd.Parameters.AddWithValue("@id", custinfo.Id);
cmd.Parameters.AddWithValue("@firstname", custinfo.Firstname);
cmd.Parameters.AddWithValue("@lastname", custinfo.Lastname);
cmd.Parameters.AddWithValue("@age", custinfo.Age);
int i = cmd.ExecuteNonQuery();
con.Close();
}
}

IService1.cs:-
[ServiceContract]
public interface IService1
{
[OperationContract]
string InsertCustomerDetails(CustomerDetails userInfo);
}

[DataContract]
public class CustomerDetails
{
int id;

[DataMember]
public int Id
{
get { return id; }
set { id = value; }
}
string firstname;

[DataMember]
public string Firstname
{
get { return firstname; }
set { firstname = value; }
}
string lastname;

[DataMember]
public string Lastname
{
get { return lastname; }
set { lastname = value; }
}
int age;

[DataMember]
public int Age
{
get { return age; }
set { age = value; }
}
}

推荐答案

InsertCustomerDetails方法需要返回一个你没有写过的字符串该方法的return语句。



这是唯一缺少的声明。用下面的方法替换你的Insert方法



InsertCustomerDetails method expects a string to be returned which you have not written any return statement for that method.

That is the only missing statement here. Replace your Insert method with the below method

public string InsertCustomerDetails(CustomerDetails custinfo)
{
SqlConnection con = new SqlConnection("data source:ANKUSH\\SQLEXPRESS; initial catalog=ankush");
con.Open();
SqlCommand cmd = new SqlCommand("insert into customer (Id, First_Name, Last_Name, Age) values(@id, @firstname, @lastname, @age)", con);
cmd.Parameters.AddWithValue("@id", custinfo.Id);
cmd.Parameters.AddWithValue("@firstname", custinfo.Firstname);
cmd.Parameters.AddWithValue("@lastname", custinfo.Lastname);
cmd.Parameters.AddWithValue("@age", custinfo.Age);
int i = cmd.ExecuteNonQuery();
con.Close(); 

return "Something";
}





我很确定它现在可以用了。

试一试。



I am damn sure it will work now.
Try it.


是的,当您在方法中提到字符串类型时,应该返回一些字符串,如上面的解决方案所示。
yes you should return some string when you mentioned string type in your method as the above solution suggests.


这篇关于使用WCF在Windows窗体中进行CRUD操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆