是否可以将DataSet作为字符串从WCF返回到Windows窗体?

时间:2015-01-07 19:47:53

标签: c# .net wcf

修改 我用新代码替换了旧代码!我最终使用了SqlDataReader,它运行得很好!

旧帖子: 我在VS2010中有2个解决方案,第一个是WCF服务,第二个是测试客户端应用程序。到目前为止,WCF上的大多数操作契约都是DataSet类型,bool类型或字符串类型。使用DataSet操作,我通常会检索我的testClient应用程序中的显示。对于bool和string,我将数据插入到我的数据库中。

我遇到的问题是,我正在尝试在接口中使用单个操作,该操作将根据来自testClient应用程序的某些因素运行sql语句。所以我基本上想要从我的应用程序传递2个值到服务,它将在sql语句中使用它们,然后将它从数据库获取的值返回给testClient应用程序。下面是我到目前为止,我尝试了各种各样的方法,但我最接近它的方法是将一条消息返回到一个文本框中,该文本框中写着“Table”或“System.Data.DataSet”,它应该返回一个来自数据库的URL。

Service.cs:

public string GetFileURL(DeviceDetails deviceInfo)
    {
        SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=vurdevice;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT FileURL FROM VR_Image WHERE IMEINo = @IMEINo AND FileName = @FileName", con);
        cmd.Parameters.AddWithValue("@IMEINo", deviceInfo.IMEINo);
        cmd.Parameters.AddWithValue("@FileName", deviceInfo.FileName);
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            deviceInfo.FileURL = reader.GetString(0);
        }
        con.Close();
        return deviceInfo.FileURL;
    }

IService.cs:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string InsertDetails(DeviceDetails deviceInfo);

    [OperationContract]
    DataSet GetCarrierList();

    [OperationContract]
    DataSet GetStorageList();

    [OperationContract]
    DataSet GetMainTable();

    [OperationContract]
    bool DeleteDetails(DeviceDetails deviceInfo);

    [OperationContract]
    DataSet GetStatTable();

    [OperationContract]
    DataSet GetFileURL(DeviceDetails deviceInfo);
}

[DataContract]
public class DeviceDetails
{
    string imeiNo;
    string fileName;
    string fileURL;
    string uploader;
    string carrierID;
    string storageName;
    int fileSize;
    DateTime createDate;
    string deleteID;
    DateTime deleteDate;
    string defectCode;
    string modelCode;
    string updateStatus;

    [DataMember]
    public string IMEINo
    {
        get { return imeiNo; }
        set { imeiNo = value; }
    }

    [DataMember]
    public string FileName
    {
        get { return fileName; }
        set { fileName = value; }
    }

    [DataMember]
    public string FileURL
    {
        get { return fileURL; }
        set { fileURL = value; }
    }

    [DataMember]
    public string Uploader
    {
        get { return uploader; }
        set { uploader = value; }
    }

    [DataMember]
    public string CarrierID
    {
        get { return carrierID; }
        set { carrierID = value; }
    }

    [DataMember]
    public string StorageName
    {
        get { return storageName; }
        set { storageName = value; }
    }

    [DataMember]
    public int FileSize
    {
        get { return fileSize; }
        set { fileSize = value; }
    }

    [DataMember]
    public DateTime CreateDate
    {
        get { return createDate; }
        set { createDate = value; }
    }

    [DataMember]
    public string DeleteID
    {
        get { return deleteID; }
        set { deleteID = value; }
    }

    [DataMember]
    public DateTime DeleteDate
    {
        get { return deleteDate; }
        set { deleteDate = value; }
    }

    [DataMember]
    public string DefectCode
    {
        get { return defectCode; }
        set { defectCode = value; }
    }

    [DataMember]
    public string ModelCode
    {
        get { return modelCode; }
        set { modelCode = value; }
    }

    [DataMember]
    public string UpdateStatus
    {
        get { return updateStatus; }
        set { updateStatus = value; }
    }
}

稍后在我的testClient应用程序中,我有一个WinForm ......

Show.cs

public partial class ShowImage : Form
{
    vurServiceReference.Service1Client obj = new vurServiceReference.Service1Client();
    private string strIMEI, strFile;
    public ShowImage(string strIMEI, string strFile)
    {
        this.strIMEI = strIMEI;
        this.strFile = strFile;

        InitializeComponent();
    }

    private void ShowImage_Load(object sender, EventArgs e)
    {
        string strFileURL = this.GetFileURL();

        if (strFileURL.Equals(""))
        {
            MessageBox.Show("No image available");
            return;
        }
        else
        {
            sampleBox.Text = strFileURL;
        }
    }

    private string GetFileURL()
    {
        vurServiceReference.DeviceDetails objdetail = new vurServiceReference.DeviceDetails();
        string url;

        objdetail.IMEINo = strIMEI;
        objdetail.FileName = strFile;
        url = obj.GetFileURL(objdetail);
        return url;
    }
}

0 个答案:

没有答案