How to change dateformat in VB.NET 2010?
I am using a DateTimePicker
control in my form.
By selecting a date, it should开发者_开发百科 be inserted in an MS SQL database. But, the date format in VB is (DMY) and is different from the format in MS SQL (YMD) !!
How can I resolve this?
Use parametrised query to solve the problem. The following is an example:
Dim cmd as SqlCommand = new SqlCommand(
"INSERT INTO table_name (username, logindate)
VALUES (@USERNAME, @DATE)", conn)
cmd.Parameters.AddWithValue("@USERNAME", tbUserName.Text)
cmd.Parameters.AddWithValue("@DATE", dateTimePicker.Value)
int i = cmd.ExecuteNonQuery();
However, if you are interested in formatting your date value, the following articles may help you:
Standard Date and Time Format Strings
Custom Date and Time Format Strings
精彩评论