我只是按照这个http://www.dotnetcurry.com/ShowArticle.aspx?ID=585 来做这个项目,但有错误歧义和缺少部分修饰符。
我在 Iservice1.cs 的代码
namespace WCT_DataServiceApp
{
[ServiceContract]
public interface IService1
{
[OperationContract]
ImageEmployee GetEmpByEmpNo(int EmpNo);
}
[DataContract]
public class ImageEmployee
{
[DataMember]
public int EmpNo { get; set; }
[DataMember]
public byte[] EmpImage { get; set; }
}
}
和 Service1.svc.cs 中的代码
namespace WCT_DataServiceApp
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
public ImageEmployee GetEmpByEmpNo(int EmpNo)
{
var Conn = new SqlConnection(@"Data Source=yeo-pc\sqlexpress;Initial Catalog=image;Integrated Security=True");
Conn.Open();
var Cmd = new SqlCommand();
Cmd.Connection = Conn;
Cmd.CommandText = "Select * from ImageEmployee where EmpNo=@EmpNo";
Cmd.Parameters.AddWithValue("@EmpNo", EmpNo);
var Reader = Cmd.ExecuteReader();
ImageEmployee objImgEmp = new ImageEmployee();
while (Reader.Read())
{
objImgEmp.EmpNo = Convert.ToInt32(Reader["EmpNo"]);
objImgEmp.EmpImage = (byte[])Reader["EmpImage"];
}
Reader.Close();
Conn.Close();
return objImgEmp;
}
}
}