您如何将在 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; }
}