Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# windows forms With MS SQL DataBase

C# windows forms With MS SQL DataBase

Scheduled Pinned Locked Moved C#
databasecsharpwinformssysadminquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zeyad Jalil
    wrote on last edited by
    #1

    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

    G OriginalGriffO P 3 Replies Last reply
    0
    • Z Zeyad Jalil

      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

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      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'

      Z 1 Reply Last reply
      0
      • G Garth J Lancaster

        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'

        Z Offline
        Z Offline
        Zeyad Jalil
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • Z Zeyad Jalil

          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

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • Z Zeyad Jalil

            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

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups