简体   繁体   中英

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. I am using linq to fetch the value from stored procedure. 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. 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. 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:

DataClasses1DataContext DataContext = new DataClasses1DataContext();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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