C# windows forms With MS SQL DataBase
-
Hi, I Have a C# windows application and it is connected to MS SQL database using direct connection, the database is in server and the application in a client machine (other PC). My question is when I save a transaction from client PC with datetime to a table in the sql databse on the server (DateTime.Now) get the client datetime, I need to insert the Server datetime not the client, please let me know is it possible to do this. Insert into XYZ(ID,Name,InsertDate) Values (1,'Test',DateTime.Now) I need DateTime.Now to be server datetime, and Don't need to use getDate() sql. Thank You
-
Hi, I Have a C# windows application and it is connected to MS SQL database using direct connection, the database is in server and the application in a client machine (other PC). My question is when I save a transaction from client PC with datetime to a table in the sql databse on the server (DateTime.Now) get the client datetime, I need to insert the Server datetime not the client, please let me know is it possible to do this. Insert into XYZ(ID,Name,InsertDate) Values (1,'Test',DateTime.Now) I need DateTime.Now to be server datetime, and Don't need to use getDate() sql. Thank You
its not clear how you're putting that insert together - I hope not concatenation, but you're building a parameterised query .. anyway MySQL has a date/time function NOW() MySQL Date and Time Functions[^] - my thoughts are that that is a server datetime whoops, you wanted MSSQL not MySQL - sorry .. how about SYSDATETIME() Date and Time Data Types and Functions (Transact-SQL)[^] - description looks correct 'instance on which MSSQL is running'
-
its not clear how you're putting that insert together - I hope not concatenation, but you're building a parameterised query .. anyway MySQL has a date/time function NOW() MySQL Date and Time Functions[^] - my thoughts are that that is a server datetime whoops, you wanted MSSQL not MySQL - sorry .. how about SYSDATETIME() Date and Time Data Types and Functions (Transact-SQL)[^] - description looks correct 'instance on which MSSQL is running'
Yes, it is parameterized and not a concatenation one, but I don't need to use sqldate I need to read the date from server and insert it again thanks
-
Hi, I Have a C# windows application and it is connected to MS SQL database using direct connection, the database is in server and the application in a client machine (other PC). My question is when I save a transaction from client PC with datetime to a table in the sql databse on the server (DateTime.Now) get the client datetime, I need to insert the Server datetime not the client, please let me know is it possible to do this. Insert into XYZ(ID,Name,InsertDate) Values (1,'Test',DateTime.Now) I need DateTime.Now to be server datetime, and Don't need to use getDate() sql. Thank You
If you want the actual server Date and Time value, then you have little choice but to use GETDATE() - that is the only way to get the server setting. You could "prefetch" it, but if the timestamp is important that's a bad idea and it could be out by seconds or more by the time you use it. Or you could use GETUTCDATE() which returns a universal value rather than one based on the server locale settings. If you don't want to call it directly, the other option is to add one of the two function calls as the default value for that column when you define the table - that way the system will set it for you if you don't specify it. And please, use parameterised queries rather than passing any values as part of the SQL command string directly - you leave your application wide open to SQL Injection attacks if you don't.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hi, I Have a C# windows application and it is connected to MS SQL database using direct connection, the database is in server and the application in a client machine (other PC). My question is when I save a transaction from client PC with datetime to a table in the sql databse on the server (DateTime.Now) get the client datetime, I need to insert the Server datetime not the client, please let me know is it possible to do this. Insert into XYZ(ID,Name,InsertDate) Values (1,'Test',DateTime.Now) I need DateTime.Now to be server datetime, and Don't need to use getDate() sql. Thank You
A simpler method is to not pass InsertDate across at all. Instead, set it to use a default value of GETUTCDATE(), so whenever you insert a row, the UTC date will be applied. That way, you don't have to worry about calling the server functions.
This space for rent