| | |
Problem with SqlDateTime
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 2
Reputation:
Solved Threads: 0
Hi all
Im trying to insert an SQLDateTime into an SQL database column which takes DateTime
here is an extract of my code
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 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
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)
sTime = new SqlDateTime(DateTime.Now); command.CommandText = "INSERT INTO Sensors VALUES ('" + sensors[i].getName() + "','" + sensors[i].getType() + "'," + sensors[i].getVal() + ", " + sTime.Value + " )"; 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)
sTime = new SqlDateTime(DateTime.Now);
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
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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.
You might find it easier (to read and build) if you assemble the string using the string.Format() method.
C# Syntax (Toggle Plain Text)
SqlConnection conn = new SqlConnection(); SqlDateTime sTime = new SqlDateTime(DateTime.Now); SqlCommand command = new SqlCommand( string.Format("INSERT INTO Sensors VALUES ('{0}','{1}',{2},'{3}' )" , sensors[i].getName() , sensors[i].getType() , sensors[i].getVal() , sTime.Value ) , conn ); command.ExecuteNonQuery();
Last edited by JerryShaw; May 8th, 2009 at 7:34 pm.
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)
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
![]() |
Other Threads in the C# Forum
- Previous Thread: How do I get depressed key even if my application is not in Focus?
- Next Thread: Call Class Library in windows form on click of button
| Thread Tools | Search this Thread |
.net 2007 access activedirectory algorithm array barchart bitmap box broadcast c# check checkbox client combobox connect control conversion cryptographyc#winformsencryption csharp custom database datagrid datagridview dataset datetime degrees development disabled displayingopenforms draganddrop drawing encryption enum event eventcloseformc# excel file foreach form format forms ftp function gdi+ httpwebrequest image index index-error input install java label list listbox load mandelbrot math mathematics mouseclick mysql operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox server setup sleep socket sql statistics stream string table text textbox thread time timer totaldays update user usercontrol validation visual visualstudio webbrowser windows winforms wpf xml






