How to set date format from VB.NET form
sql-server, vb.net, winforms
Solution
If you are storing the date & time information to SQL using the `DateTime` field type, then SQL should automatically convert it to the format in which it uses to store those values (YYYY-MM-DD hh:mm:ss).
If, on the other hand, you are storing them as text (varchar, etc.), then you would have to modify your code to pass the date & time the way you want it.
Perhaps something like:
Dim strDate as string = Date.Now.ToString("MM/dd/yyyy hh:mm:ss tt") 'Returns date with AM/PM'
Dim strDate as string = Date.Now.ToString("MM/dd/yyyy HHH:mm:ss") 'Returns date with military time'
Problem
I have a VB.NET form that passes the users choices to my database (SQL Server 2008). One of the values the user chooses is a date from a date picker object. This date is stored in the database as a string. I've noticed that on different deployments the date format inserted into the database is different and that it is dependent on the data format set on the server. This can be a real problem for me since I need to install our process on several servers (for different clients) and I don't necessarily have the option of changing the servers date format. Is there a way to programmaticly define the date format for the output to the database?