Click here to Skip to main content
15,896,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I want to access a stored procedure and get its value from DTO. I have the following codes in DTO, can I access the stored procedure and get its returned value(UTC datetime in this case)? Below are my codes:

C#
[DataMember(Name = "g7")]
public DateTime DateEffectiveIn
{
    get
    {
        if (Environment.MachineName != StationCreatedAt && _readCount == 0)
        {
            RegenerateServerDate(); //calling the function here
            _readCount++;
        }

        return _dateEffectiveIn;
    }
    set
    {
        _dateEffectiveIn = value;
    }
}


public void RegenerateServerDate()
{
    if (ServerDateTimeSource != string.Empty)
    {
       string connetionString =           ConfigurationManager.ConnectionStrings["abcString"].ConnectionString;
       SqlConnection connection  ;
       SqlCommand command ;

       connection = new SqlConnection(connetionString);

       connection.Open();

       command = new SqlCommand("usp_GetUTCDate_Select", connection);
       command.CommandType = CommandType.StoredProcedure;

       DateTime DateUtc = command.ExecuteScalar(); //only one data returned

       command.Dispose();
       connection.Close();
           

       this.DateEffectiveIn = DateUtc;
       this.DateCreated = DateUtc;
       this.DateLastModified = DateUtc;
    
    }
    else
    {
       DateTime dateTime = DateHelper.ConvertToUtc(DateTime.Now);
       this.DateEffectiveIn = dateTime;
       this.DateCreated = dateTime;
       this.DateLastModified = dateTime;
    }
}
Posted
Comments
Sinisa Hajnal 27-Apr-15 6:08am    
Looks correct. What is the problem?

You should dispose and close the connection in finally block or it will remain open if there is an exception.
Jamie888 27-Apr-15 22:42pm    
Sir, for your information, the above codes are written on DTO layer. My senior told me that I cant access/call database from DTO layer. I could run it on local server(my PC) but not on the remote server as according to my senior. I have run through for online resources but none of the articles can answer my question. Any other inputs are appreciated. Thank you.
Sinisa Hajnal 28-Apr-15 2:31am    
That is simply not true, database access depends only on connection string and "openess" of the network (firewall, ports etc). The fact is that you SHOULDN'T access the database, DTO shouldn't have any behaviour, just the data. But you could.

If I remember correctly DTO should serve as simple "dumb" data container that is filled through various service calls by other objects, by itself it shouldn't do anything.

Depends on the project and your interpretation of D(ata) T(ransfer) O(bject).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900