0

您如何将在 SQL Server 2012 中具有DateTime类型的列带入 WCF DataContract C# 类和SqlDataReader. 我必须先转换它吗?它在界面中的事实是否有所作为?

WCF 服务是我SqlConnection在 asp.net 网格视图中的替代品,我对那里的所有信息感到困惑DateTime,只是想知道,因为到目前为止我所了解的是,在 C#DateTime中不被视为实际的数据类型,那么这将如何工作。

如果我把它变成一个字符串,它说它不能这样做,那么此时该怎么办?感谢您提供的任何帮助,以帮助理解这一点,并帮助我了解我可以采取的路线。

沮丧和无知,琳达

这是我的InvoiceDB

namespace AbbeyRoadServiceLibrary
{
   [DataObject(true)]
   public class InvoiceDB
   {
      [DataObjectMethod(DataObjectMethodType.Select)]
      public static List<Invoice> GetInvoices(string customerid)
      {
          List<Invoice> invoiceList = new List<Invoice>();

          SqlConnection con = AbbeyRoadDB.GetConnection();

          SqlCommand cmd = new SqlCommand();
          cmd.CommandText = "select InvoiceNum,InvoiceDate,InvoiceTotal,Job,Status from Invoices where CustomerID = @CustomerID order by InvoiceNum";
          cmd.CommandType = CommandType.Text;
          cmd.Connection = con;
          cmd.Parameters.AddWithValue("@CustomerID", customerid);

          try
          {
              con.Open();
              SqlDataReader reader = cmd.ExecuteReader();

              while (reader.Read())
              {
                  Invoice i = new Invoice();
                  i.InvoiceNum = reader[0].ToString();
             // Thought I was to convert it but no happy code results from doing this in this  way!!
                  i.InvoiceDate = Convert.ToDateTime(reader[1]); 
                  i.InvoiceTotal = Convert.ToDecimal(reader[2]);
                  i.Job = reader[3].ToString();
                  i.Status = reader[4].ToString();

                  invoiceList.Add(i);
              }
              reader.Close();
          }
          catch (SqlException ex)
          {
              throw ex;
          }
          finally
          {
              con.Close();
          }

          return invoiceList;
      }
   }
}

我的界面代码主要只是重要的部分!

public interface InterfaceInvoiceService
{
    [OperationContract]
    List<Customer> GetCustomer();

    [OperationContract]
    List<Invoice> GetInvoices(string customerid);
}

[DataContract]
public class Invoice
{
    string invoiceID = "";
    string invoicedate = "";  //I know this is not right but see below
    decimal invoicetotal = 0m;
    string job = "";
    string status ="";

    [DataMember]
    public string InvoiceNum
    {
        get { return invoiceID; }
        set { invoiceID = value; }
    }

    [DataMember]
    public DateTime InvoiceDate
    {
        get { return invoicedate; } // here it says that it cannot implicitly convert it to a string on return and value.
        set { invoicedate = value; }
    }

    [DataMember]
    public decimal InvoiceTotal
    {
        get { return invoicetotal; }
        set { invoicetotal = value; }
    }

    [DataMember]
    public string Job
    {
        get { return job; }
        set { job = value; }
    }
4

2 回答 2

1

在 C# 中,DateTime 一种数据类型。此外,您可以使用auto-properties来缩短代码:

[DataContract]
public class Invoice
{
    [DataMember]
    public string InvoiceNum { get; set; }

    [DataMember]
    public DateTime InvoiceDate { get; set; }

    [DataMember]
    public decimal InvoiceTotal { get; set; }

    [DataMember]
    public string Job { get; set; }
}
于 2013-05-05T15:53:37.143 回答
0

将变量的数据类型更改为 DateTime 作为

DateTime invoicedate;

[DataMember]
public DateTime InvoiceDate
{
    get { return invoicedate; } 
    set { invoicedate = value; }
}
于 2013-05-05T15:44:32.437 回答