943,800 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3128
  • C# RSS
May 8th, 2009
0

Problem with SqlDateTime

Expand Post »
Hi all

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

here is an extract of my code
C# Syntax (Toggle Plain Text)
  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
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
finnj6 is offline Offline
4 posts
since May 2009
May 8th, 2009
0

Re: Problem with SqlDateTime

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.
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
May 8th, 2009
0

Re: Problem with SqlDateTime

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.

C# Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
May 11th, 2009
0

Re: Problem with SqlDateTime

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
finnj6 is offline Offline
4 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC