G
Guest
I have a Datetime field in a Sql Server 2000 database, and need to update
this from within a Winforms application.
I have found the format of the datetime value sent to Sql server is
different according to whether I send the value directly or use a
SqlParameter. In the former case I get an error message if the day or month
is higher than 12, as the 'm/d/y' date format appears to be used when Sql
server receives the data, whereas it is expecting a 'd/m/y' format (although
it is sent in the correct format if SqlParameter is used). I am in the UK
and have my computer regional setting set to UK.
I used the following code to demonstrate the problem (form with only a
button1 and checkbox1):
private void button1_Click(object sender, EventArgs e)
{
DateTime dt = dateTimePicker1.Value;
SqlCommand com = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionstring);
com.Connection = conn;
conn.Open();
if (checkBox1.Checked == true)
{
com.Parameters.Add(new SqlParameter("@dt" ,dt));
com.CommandText = @"INSERT INTO [dbo].[Test]([DueDate]) VALUES
(@dt)"; //OK
}
else
{
com.CommandText = @"INSERT INTO [dbo].[Test]([DueDate]) VALUES
('" + dateTimePicker1.Value.ToString() + "')"; //error if
day/month > 12
}
com.ExecuteNonQuery();
conn.Close();
}
Can anyone please help me understand why this is happening. I would expect
UK formats to be used throughout by default. I am using VS 2005 beta 2.
Thanks
this from within a Winforms application.
I have found the format of the datetime value sent to Sql server is
different according to whether I send the value directly or use a
SqlParameter. In the former case I get an error message if the day or month
is higher than 12, as the 'm/d/y' date format appears to be used when Sql
server receives the data, whereas it is expecting a 'd/m/y' format (although
it is sent in the correct format if SqlParameter is used). I am in the UK
and have my computer regional setting set to UK.
I used the following code to demonstrate the problem (form with only a
button1 and checkbox1):
private void button1_Click(object sender, EventArgs e)
{
DateTime dt = dateTimePicker1.Value;
SqlCommand com = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionstring);
com.Connection = conn;
conn.Open();
if (checkBox1.Checked == true)
{
com.Parameters.Add(new SqlParameter("@dt" ,dt));
com.CommandText = @"INSERT INTO [dbo].[Test]([DueDate]) VALUES
(@dt)"; //OK
}
else
{
com.CommandText = @"INSERT INTO [dbo].[Test]([DueDate]) VALUES
('" + dateTimePicker1.Value.ToString() + "')"; //error if
day/month > 12
}
com.ExecuteNonQuery();
conn.Close();
}
Can anyone please help me understand why this is happening. I would expect
UK formats to be used throughout by default. I am using VS 2005 beta 2.
Thanks