Don't build queries dynamically like that, is it dangerous and hurts performance. You do not need to instantiate a new SqlDateTime(otherDateTime), just set the parameter to the CLR DateTime and let the data drivers map it for you.
private void simpleButton1_Click(object sender, EventArgs e)
{
const string query = "Insert Into aTable (aString, aDateTime) Values (@aString, @aDateTime)";
const string connStr = @"Data Source=apex2006sql;Initial Catalog=DB;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
string s1 = "abc123";
DateTime dtNow = DateTime.Now;
cmd.Parameters.Add(new SqlParameter(@"aString", SqlDbType.VarChar)).Value = s1;
cmd.Parameters.Add(new SqlParameter(@"aDateTime", SqlDbType.DateTime)).Value = dtNow;
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
Last edited by sknake; May 8th, 2009 at 10:35 pm. Reason: typo2
Reputation Points: 1749
Solved Threads: 735
Senior Poster
Offline 3,948 posts
since Feb 2009