Click here to Skip to main content
12,064,498 members (64,417 online)
Rate this:
 
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 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 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 17-Jan-13 5:08am
   
I am getting error in above your statement.
Invalid Expresstion ","
Marcus Kramer 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160204.4 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2016
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