Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server time
I am saving values to a database but I am not able to save them with current Time.
 
I have created a colume with name Time in database, but dont know how to save time.
 

SqlConnection sqlconn  = new SqlConnection("Data Source=AAN-PC;Initial Catalog=latestsensordata;Integrated Security=True");
       SqlDataAdapter sa = new SqlDataAdapter();
       string query = "insert into Table_1(SensorValue,Time) values(" +hexValue+")";
                        sa.InsertCommand = new SqlCommand(query, sqlconn);
Posted 16-Jan-13 23:53pm
Edited 17-Jan-13 0:32am
v4
Comments
Teju MB at 17-Jan-13 5:02am
   
What is "hexvalue" here??
You are passing only one parameter to your INSERT statement while mentioning it as two.It astounds .. !
Amir Mahfoozi at 21-Jan-13 1:04am
   
Hi,
why don't you define a default value of "getdate()" for the time field at the database level ?
You can ignore this field when inserting new data to the table. It automatically gets server time of insertion.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
 
For consistency I would assume you require the server time and not the client pc time.
 
All you need to do is set the Time on your table with a default of Getdate().
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

HI,
 
Here you are passing only session values.
 
string query = "insert into Table_1(SensorValue,Time) values(" +hexValue+ "," +DateTime.Now.ToString("HH:MM:ss")+")";
 
Thanks
  Permalink  
v2
Comments
ontheline89 at 17-Jan-13 5:08am
   
I am getting error in above your statement.
Invalid Expresstion ","
Marcus Kramer at 17-Jan-13 9:59am
   
Paramatarized Queries are the way to go. We should NEVER suggest inline values like this. They are nothing but an invitation to SQL injection.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi,
 
For consistency I would assume you require the server time and not the client pc time.
 
All you need to do is set the Time on your table with a default of Getdate().
 
In this way you don't pass any value on insert.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Pass the value datetime.now() value to time. Then it inserts a value along with current time.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

i think this will help You..
  SqlConnection sqlconn = new SqlConnection("Data Source=AAN-PC;Initial Catalog=latestsensordata;Integrated Security=True");
                        SqlDataAdapter sa = new SqlDataAdapter();
                        string CurrntTime = System.DateTime.Now.TimeOfDay.ToString();
                        string query = "insert into Table_1(SensorValue,Time) values(" + hexValue + ",CurrntTime)";
                        sa.InsertCommand = new SqlCommand(query, sqlconn);
  Permalink  
v2
Comments
ontheline89 at 17-Jan-13 5:23am
   
I am getting error,
Invalid column name CurrntTime.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 386
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 215
3 /\jmot 189
4 Praneet Nadkar 173
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100