简体   繁体   English

使用linqToSql从存储过程中的值获取C#中的类

[英]Get values from stored procedure to class in c# using linqToSql

This is my c# code that will take server date from database using Stored procedure. 这是我的C#代码,它将使用存储过程从数据库获取服务器日期。 I am using linq to fetch the value from stored procedure. 我正在使用linq从存储过程中获取值。 On data context, while executing the stored procedure, date and time is shown. 在数据上下文中,在执行存储过程时,将显示日期和时间。 But when I keep the code in class, return value is 0.Can anyone help me. 但是当我将代码保存在类中时,返回值为0。任何人都可以帮助我。 This is my code, 这是我的代码,

public class NetworkTime
{
       CheckoutDataContext DataContext = new CheckoutDataContext();
       public DateTime GetDateTimeFromServer()
       {
             var  date = DataContext.SPGetDate().ReturnValue;
             return Convert.ToDateTime(date);
       }
}

SPGetDate is the name of stored procedure. SPGetDate是存储过程的名称。 Can anyone tell how can I get date and time!!! 谁能告诉我如何获取日期和时间!

I guess you want to retrieve database current date and time. 我猜您想检索数据库的当前日期和时间。 I suggest instead of wrapping sqlserver getdate() in a sp I suggest you do the following: 我建议不要将sqlserver getdate()包装在sp中,我建议您执行以下操作:

DataClasses1DataContext DataContext = new DataClasses1DataContext();

    var result = DataContext.ExecuteQuery<DateTime>("SELECT GETDATE()").First();
    Console.ReadLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM