Problem with SqlDateTime

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 2
Reputation: finnj6 is an unknown quantity at this point 
Solved Threads: 0
finnj6 finnj6 is offline Offline
Newbie Poster

Problem with SqlDateTime

 
0
  #1
May 8th, 2009
Hi all

Im trying to insert an SQLDateTime into an SQL database column which takes DateTime

here is an extract of my code
  1. sTime = new SqlDateTime(DateTime.Now);
  2. command.CommandText = "INSERT INTO Sensors VALUES ('" + sensors[i].getName() + "','" + sensors[i].getType() + "'," + sensors[i].getVal() + ", " + sTime.Value + " )";
  3. command.ExecuteNonQuery();

The error I'm getting is "Incorrect syntax near '10'. "
10 is the hour it is at this moment it seems to process the date ok but gets stuck after the hour the format of sTime after
  1. sTime = new SqlDateTime(DateTime.Now);
29/01/2002 10:28:49

Im really stumped by this and databases arn't my strong point, after this line of code is solved thats the end of my final year project in college. So help would be greatly appreciated.

Thanks
Jeff
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Problem with SqlDateTime

 
0
  #2
May 8th, 2009
Make sure your DateTime value is enclosed in single quotes.

You might find it easier (to read and build) if you assemble the string using the string.Format() method.
  1. SqlConnection conn = new SqlConnection();
  2. SqlDateTime sTime = new SqlDateTime(DateTime.Now);
  3. SqlCommand command = new SqlCommand(
  4. string.Format("INSERT INTO Sensors VALUES ('{0}','{1}',{2},'{3}' )"
  5. , sensors[i].getName()
  6. , sensors[i].getType()
  7. , sensors[i].getVal()
  8. , sTime.Value
  9. )
  10. , conn
  11. );
  12. command.ExecuteNonQuery();
Last edited by JerryShaw; May 8th, 2009 at 7:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,211
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 572
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Problem with SqlDateTime

 
0
  #3
May 8th, 2009
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.

  1. private void simpleButton1_Click(object sender, EventArgs e)
  2. {
  3. const string query = "Insert Into aTable (aString, aDateTime) Values (@aString, @aDateTime)";
  4. const string connStr = @"Data Source=apex2006sql;Initial Catalog=DB;Integrated Security=True;";
  5. using (SqlConnection conn = new SqlConnection(connStr))
  6. {
  7. conn.Open();
  8. using (SqlCommand cmd = new SqlCommand(query, conn))
  9. {
  10. string s1 = "abc123";
  11. DateTime dtNow = DateTime.Now;
  12. cmd.Parameters.Add(new SqlParameter(@"aString", SqlDbType.VarChar)).Value = s1;
  13. cmd.Parameters.Add(new SqlParameter(@"aDateTime", SqlDbType.DateTime)).Value = dtNow;
  14. cmd.ExecuteNonQuery();
  15. }
  16. conn.Close();
  17. }
  18. }
Last edited by sknake; May 8th, 2009 at 10:35 pm. Reason: typo2
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: finnj6 is an unknown quantity at this point 
Solved Threads: 0
finnj6 finnj6 is offline Offline
Newbie Poster

Re: Problem with SqlDateTime

 
0
  #4
May 11th, 2009
Thanks for the help I got into my lab last night and got it going. I'm so happy to get it working. I ended up taking sknake's advice and built the command using parameters.

Hopefully I'll get a decent mark now

Jeff
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC