簡體   English   中英

如何將SQL Server 2012中具有DateTime類型的列引入WCF DataContract C#類中?

[英]How to bring a column that has a DateTime type in SQL Server 2012 into a WCF DataContract C# class?

如何將SQL Server 2012中具有DateTime類型的列引入WCF DataContract C#類和SqlDataReader 我需要先將其轉換嗎? 它在接口中的事實是否有所作為?

WCF服務是我在asp.net網格視圖中替代SqlConnection的工具,我對那里有關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; }
    }

在C#中, DateTime 數據類型。 另外,您可以使用自動屬性來縮短代碼:

[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; }
}

將變量的數據類型更改為DateTime為

DateTime invoicedate;

[DataMember]
public DateTime InvoiceDate
{
    get { return invoicedate; } 
    set { invoicedate = value; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM