问题描述
我收到一条编译错误消息,在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; }
}
}
推荐答案
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.
这篇关于使用WCF在Windows窗体中进行CRUD操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!