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. Insert byte[] to database

Insert byte[] to database

Scheduled Pinned Locked Moved C#
databasequestion
8 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.
  • D Offline
    D Offline
    dataminers
    wrote on last edited by
    #1

    How can I write sql sentence with byte[] parameter? NOTE: I don't want to use SqlParameter. string sql = "INSERT INTO MyFile(FileId, BinaryFile) VALUES('" + fileId + "','" + file + "')"; .... command.ExecuteNonQuery(); fieldId is int variable, file is byte[] variable. In database; FileId is int field, file is varbinary(MAX) field.

    L P T M A 5 Replies Last reply
    0
    • D dataminers

      How can I write sql sentence with byte[] parameter? NOTE: I don't want to use SqlParameter. string sql = "INSERT INTO MyFile(FileId, BinaryFile) VALUES('" + fileId + "','" + file + "')"; .... command.ExecuteNonQuery(); fieldId is int variable, file is byte[] variable. In database; FileId is int field, file is varbinary(MAX) field.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      http://dev.mysql.com/doc/refman/5.0/es/connector-net-using-blob.html[^]

      D 1 Reply Last reply
      0
      • L Lost User

        http://dev.mysql.com/doc/refman/5.0/es/connector-net-using-blob.html[^]

        D Offline
        D Offline
        dataminers
        wrote on last edited by
        #3

        I don't want to use SQL Parameter. I use MS SQL.

        P 1 Reply Last reply
        0
        • D dataminers

          How can I write sql sentence with byte[] parameter? NOTE: I don't want to use SqlParameter. string sql = "INSERT INTO MyFile(FileId, BinaryFile) VALUES('" + fileId + "','" + file + "')"; .... command.ExecuteNonQuery(); fieldId is int variable, file is byte[] variable. In database; FileId is int field, file is varbinary(MAX) field.

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

          First of all, please don't use sql like this. As you are using SQL Server, you should really use command parameters (this helps circumvent SQL Injection Attacks). Then you could set the data easily, as detailed here:

          using (SqlCommand cmd = new SqlCommand("MyFile(FileId, BinaryFile) VALUES(@fileid, @file", connection)
          {
          cmd.Parameters.AddWithValue("@fileid", fileid);
          cmd.Parameters.AddWithValue("@file", file) ;
          cmd.ExecuteNonQuery();
          }

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • D dataminers

            I don't want to use SQL Parameter. I use MS SQL.

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

            Tough, you have to. And it's the right thing to do, every time.

            1 Reply Last reply
            0
            • D dataminers

              How can I write sql sentence with byte[] parameter? NOTE: I don't want to use SqlParameter. string sql = "INSERT INTO MyFile(FileId, BinaryFile) VALUES('" + fileId + "','" + file + "')"; .... command.ExecuteNonQuery(); fieldId is int variable, file is byte[] variable. In database; FileId is int field, file is varbinary(MAX) field.

              T Offline
              T Offline
              T M Gray
              wrote on last edited by
              #6

              That's like saying you want to swim but you don't want to get wet. Maybe if you tell us your reasons why you don't want to use a parameter object we could help you with a solution that better fits your needs. Otherwise it isn't worth coming up with an answer because who knows what other objects you refuse to use for no apparant reason.

              1 Reply Last reply
              0
              • D dataminers

                How can I write sql sentence with byte[] parameter? NOTE: I don't want to use SqlParameter. string sql = "INSERT INTO MyFile(FileId, BinaryFile) VALUES('" + fileId + "','" + file + "')"; .... command.ExecuteNonQuery(); fieldId is int variable, file is byte[] variable. In database; FileId is int field, file is varbinary(MAX) field.

                M Offline
                M Offline
                Matt Meyer
                wrote on last edited by
                #7

                Like other have said, unless you're really experienced with SQL, you should use parameters (and even if you are really experienced, it's still a good idea). The first problem is you appear to be using the wrong data type to store bytes. VARBINARY is a character type to store binary strings (useful for forcing case sensitivity, IE, 'a'='A' in a char type, but not a binary type) - http://dev.mysql.com/doc/refman/5.1/en/binary-varbinary.html[^] You probably wanted a BLOB type of some kind. As for storing byte[] with an SQL statement, converting the bytes into a hexadecimal string value should work - INSERT INTO (FileID, Data) VALUES (1, 0x0F2EAA32);

                1 Reply Last reply
                0
                • D dataminers

                  How can I write sql sentence with byte[] parameter? NOTE: I don't want to use SqlParameter. string sql = "INSERT INTO MyFile(FileId, BinaryFile) VALUES('" + fileId + "','" + file + "')"; .... command.ExecuteNonQuery(); fieldId is int variable, file is byte[] variable. In database; FileId is int field, file is varbinary(MAX) field.

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #8

                  http://www.codeproject.com/Questions/83971/I-need-to-save-a-file-or-bytes-in-a-database-WITHO.aspx Strange your question was asked just hours earlier. I wonder why that is.

                  [Forum Guidelines]

                  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