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. Create an Insert SQL - Statement for insert a date

Create an Insert SQL - Statement for insert a date

Scheduled Pinned Locked Moved C#
databasecsharptutorialhelpquestion
10 Posts 7 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.
  • N Offline
    N Offline
    neus83
    wrote on last edited by
    #1

    Hi all, I was trying and trying ... in Visual c# .net I use a date which should besaved to the database: DateTime TimeForTheDatabase = DateTime.Now; ... this is only an example ... Now I will Insert "TimeForTheDatabase" to the Database: SqlCommand dataCommand = new SqlCommand("Insert INTO table1 (Text,Datum) VALUES ("Info",TimeForTheDatabase)",dataConnection); But I get an Error :( Can somebody give me a sample how to insert my "TimeForTheDatabase" into my DB ? Thanks

    A S OriginalGriffO C 4 Replies Last reply
    0
    • N neus83

      Hi all, I was trying and trying ... in Visual c# .net I use a date which should besaved to the database: DateTime TimeForTheDatabase = DateTime.Now; ... this is only an example ... Now I will Insert "TimeForTheDatabase" to the Database: SqlCommand dataCommand = new SqlCommand("Insert INTO table1 (Text,Datum) VALUES ("Info",TimeForTheDatabase)",dataConnection); But I get an Error :( Can somebody give me a sample how to insert my "TimeForTheDatabase" into my DB ? Thanks

      A Offline
      A Offline
      Arindam Tewary
      wrote on last edited by
      #2

      whats the error????? without error description how people can help you ?

      Thanks, Arindam D Tewary

      1 Reply Last reply
      0
      • N neus83

        Hi all, I was trying and trying ... in Visual c# .net I use a date which should besaved to the database: DateTime TimeForTheDatabase = DateTime.Now; ... this is only an example ... Now I will Insert "TimeForTheDatabase" to the Database: SqlCommand dataCommand = new SqlCommand("Insert INTO table1 (Text,Datum) VALUES ("Info",TimeForTheDatabase)",dataConnection); But I get an Error :( Can somebody give me a sample how to insert my "TimeForTheDatabase" into my DB ? Thanks

        S Offline
        S Offline
        Shivendra Pandey
        wrote on last edited by
        #3

        the problem is format. if u simply want to store datatime do in in db side example Insert INTO table1 (Text,Datum) VALUES ("Info",getdate())

        1 Reply Last reply
        0
        • N neus83

          Hi all, I was trying and trying ... in Visual c# .net I use a date which should besaved to the database: DateTime TimeForTheDatabase = DateTime.Now; ... this is only an example ... Now I will Insert "TimeForTheDatabase" to the Database: SqlCommand dataCommand = new SqlCommand("Insert INTO table1 (Text,Datum) VALUES ("Info",TimeForTheDatabase)",dataConnection); But I get an Error :( Can somebody give me a sample how to insert my "TimeForTheDatabase" into my DB ? Thanks

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

          OK, there are a few problems here. Do it this way:

          SqlCommand dataCommand = new SqlCommand("INSERT INTO table1 (Text,Datum) VALUES (@TX,@DT)");
          dataCommand.Parameters.AddWithValue("@TX", "Info");
          dataCommand.Parameters.AddWithValue("@DT", TimeForTheDatabase);

          That way you won't get the syntax error, and you can start to avoid SQL injection attacks later on. [edit]I forgot the .Parameters bit. :doh: [/edit]

          All those who believe in psycho kinesis, raise my hand.

          modified on Thursday, December 17, 2009 10:07 AM

          "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

          L 1 Reply Last reply
          0
          • N neus83

            Hi all, I was trying and trying ... in Visual c# .net I use a date which should besaved to the database: DateTime TimeForTheDatabase = DateTime.Now; ... this is only an example ... Now I will Insert "TimeForTheDatabase" to the Database: SqlCommand dataCommand = new SqlCommand("Insert INTO table1 (Text,Datum) VALUES ("Info",TimeForTheDatabase)",dataConnection); But I get an Error :( Can somebody give me a sample how to insert my "TimeForTheDatabase" into my DB ? Thanks

            C Offline
            C Offline
            cysccnu
            wrote on last edited by
            #5

            That is because the time format isn't converted to string. Try this: SqlCommand dataCommand = new SqlCommand(string.Format("Insert INTO table1 (Text,Datum) VALUES ('Info',{0})",TimeForTheDatabase),dataConnection);

            P OriginalGriffO 2 Replies Last reply
            0
            • OriginalGriffO OriginalGriff

              OK, there are a few problems here. Do it this way:

              SqlCommand dataCommand = new SqlCommand("INSERT INTO table1 (Text,Datum) VALUES (@TX,@DT)");
              dataCommand.Parameters.AddWithValue("@TX", "Info");
              dataCommand.Parameters.AddWithValue("@DT", TimeForTheDatabase);

              That way you won't get the syntax error, and you can start to avoid SQL injection attacks later on. [edit]I forgot the .Parameters bit. :doh: [/edit]

              All those who believe in psycho kinesis, raise my hand.

              modified on Thursday, December 17, 2009 10:07 AM

              L Offline
              L Offline
              loyal ginger
              wrote on last edited by
              #6

              I am not sure SqlCommand has a AddWithValue method. You are supposed to add to the Parameters collection, not to the command object.

              OriginalGriffO 1 Reply Last reply
              0
              • L loyal ginger

                I am not sure SqlCommand has a AddWithValue method. You are supposed to add to the Parameters collection, not to the command object.

                OriginalGriffO Online
                OriginalGriffO Online
                OriginalGriff
                wrote on last edited by
                #7

                :doh: Corrected - thanks!

                All those who believe in psycho kinesis, raise my hand.

                "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
                • C cysccnu

                  That is because the time format isn't converted to string. Try this: SqlCommand dataCommand = new SqlCommand(string.Format("Insert INTO table1 (Text,Datum) VALUES ('Info',{0})",TimeForTheDatabase),dataConnection);

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  No, never do that. Use parameters.

                  1 Reply Last reply
                  0
                  • C cysccnu

                    That is because the time format isn't converted to string. Try this: SqlCommand dataCommand = new SqlCommand(string.Format("Insert INTO table1 (Text,Datum) VALUES ('Info',{0})",TimeForTheDatabase),dataConnection);

                    OriginalGriffO Online
                    OriginalGriffO Online
                    OriginalGriff
                    wrote on last edited by
                    #9

                    Not really recommended - firstly beacause it involves an unnecessary string conversion, and secondly because it is better practise to use parameters at all times. In this case there is no risk of an SQL injection attack, but all it would take is a small change to the logic... See the XKCD cartoon "Bobby Tables"[^]

                    All those who believe in psycho kinesis, raise my hand.

                    "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

                    P 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Not really recommended - firstly beacause it involves an unnecessary string conversion, and secondly because it is better practise to use parameters at all times. In this case there is no risk of an SQL injection attack, but all it would take is a small change to the logic... See the XKCD cartoon "Bobby Tables"[^]

                      All those who believe in psycho kinesis, raise my hand.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      Yes, for those reasons, plus performance in a loop: If you concatenate SQL strings to insert a million such rows the server has to prepare a million SQL statements, but by using a parameterized statement the server prepares the statement once and uses the cached execution plan a million times (at least in theory).

                      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