Frage

I'm developing a c# program and i need to store some date in a sql server database. In my able I have a column type date where i need to store year, month and day. So I convert the date in c# program from dd/mm/yyyy (italian format) to yyyy-mm-dd and the I insert it in the database. When I go to read the data from the table and I display it inside the textbox i see the date followed by hours:minutes:seconds. Why? In my database I only store yyyy-mm-dd (I check it in sql management studio) and in c# program i use a string type to save the date in a property of an object:

    public string data_fattura
    {
        get { return checkStringGet(_data_fattura); }
        set { _data_fattura = checkDataSet(value); }
    }    

    private string checkDataSet(string data)
    {
        string[] tmp = data.Split('/');

        return tmp[2] + "-" + tmp[1] + "-" + tmp[0];
    }

    private string checkStringGet(string data)
    {
        string[] tmp = data.Split('-');

        return tmp[2] + "/" + tmp[1] + "/" + tmp[0];
    }
War es hilfreich?

Lösung

Don't convert it to a string at all where you don't need to.

Convert from the string you get from the user using DateTime.ParseExact, and then keep it as a DateTime. Use that as a parameter in parameterized SQL to insert it into the database. The database won't be storing it in a text format anyway.

When you fetch it from the database, you'll also have a DateTime. (You can just cast the value from the reader - you don't need to go anywhere near a text representation when fetching from the database.) When you want to display that to the user, format it appropriately (using ToString).

Basically, keep the value as a DateTime (or DateTimeOffset, potentially) for as much of the time as possible... as early as you can when you get the text from the user (ideally, use a control which is date/time aware so you don't even need it then) and as late as you can when you present it back to the user.

As for the time appearing - it's not entirely clear where it was appearing. If you only want a date, you should have a date column rather than datetime. You'll still fetch the value back as a DateTime .NET value, but when you convert that to a string, just use a format which is date-only. .NET doesn't have a type which is "just a date" (unfortunately, IMO).

Andere Tipps

In my database I only store yyyy-mm-dd (I check it in sql management studio)

Nope. If it is a datetime column, it is stored in some internal format that always includes date and time information, even if the latter might be 00:00:00.

If you want to convert the value from the database to a string for displaying, use the ToString method to convert it into a format of your choice. In your case, you might want to use one without a time component:

string myStringToShow = myDateTimeFromTheDatabase.ToString("d");

The following MSDN page explains the meaning of d and tells you which other date format specifiers you can use:

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top