locked
Saving Date to SQL Database RRS feed

  • Question

  • Hello Developers !

    I have an application that’s been connected with a SQL Database, SQL Database has a table which contains a column named Date whose datatype is date.

    Now in my table we have a textbox named date, we want that when we enter a date in that textbox our date in the textbox should be saved to the database in the column by maintaining the datatype compatibility.

    I already have the code of how to save data to my database but don’t know how to convert string to the date which sql server supports.

    Any help will be appreciated.

    Thanks

    Monday, May 20, 2013 1:52 PM

Answers

  • By using a parameterized command object where the parameter for the data is setup properly the database will take care of the format if you pass a valid date.

    MSDN http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Proposed as answer by Cor Ligthert Monday, May 20, 2013 6:20 PM
    • Marked as answer by Youen Zen Wednesday, June 5, 2013 5:29 AM
    Monday, May 20, 2013 2:18 PM
  • Look in addition of the reply from Kevin to this page on our website.

    http://www.vb-tips.com/OleDBParameters.aspx


    Success
    Cor

    • Marked as answer by Youen Zen Wednesday, June 5, 2013 5:29 AM
    Monday, May 20, 2013 6:20 PM
  • Cor,

    You might want to add a Locale Specifier to that connection string as the example states it's using International English notation for one of the dates without using a date which would necessarily require parsing as en-GB (like 31/12/1995): "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Locale Identifier=2057;
    Uid=Admin;Pwd=;". (I'd quote www.connectionstrings.com/access on that, but it was me who submitted that connection string.)

    --
    Andrew

    • Marked as answer by Youen Zen Wednesday, June 5, 2013 5:29 AM
    Monday, May 20, 2013 7:43 PM

All replies

  • By using a parameterized command object where the parameter for the data is setup properly the database will take care of the format if you pass a valid date.

    MSDN http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Proposed as answer by Cor Ligthert Monday, May 20, 2013 6:20 PM
    • Marked as answer by Youen Zen Wednesday, June 5, 2013 5:29 AM
    Monday, May 20, 2013 2:18 PM
  • Look in addition of the reply from Kevin to this page on our website.

    http://www.vb-tips.com/OleDBParameters.aspx


    Success
    Cor

    • Marked as answer by Youen Zen Wednesday, June 5, 2013 5:29 AM
    Monday, May 20, 2013 6:20 PM
  • Cor,

    You might want to add a Locale Specifier to that connection string as the example states it's using International English notation for one of the dates without using a date which would necessarily require parsing as en-GB (like 31/12/1995): "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Locale Identifier=2057;
    Uid=Admin;Pwd=;". (I'd quote www.connectionstrings.com/access on that, but it was me who submitted that connection string.)

    --
    Andrew

    • Marked as answer by Youen Zen Wednesday, June 5, 2013 5:29 AM
    Monday, May 20, 2013 7:43 PM