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. Database & SysAdmin
  3. Database
  4. store a c# byte array

store a c# byte array

Scheduled Pinned Locked Moved Database
databasecsharpsysadmindata-structureshelp
5 Posts 3 Posters 2 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.
  • P Offline
    P Offline
    pelos
    wrote on last edited by
    #1

    hello forum, i want to store c# objects in a MS SQL database. I think the best way is to serialize the object to a byte array but i have problems to insert a c# byte array into the database... First, the equivalent type of datum in MS SQL is the binary or varbinary type, isn´t it? well, my code: MyObject mo = new MyObject(); MemoryStream ms= new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); b.Serialize(ms,mo); SqlConnection sc = new SqlConnection("server=localhost;uid=sa;pwd=;database=myDB"); sc.Open(); SqlDataAdapter sda = new SqlDataAdapter("select * from myTable",sc); DataSet ds = new DataSet(); ad.Fill(ds,"tableFile"); DataTable dt = ds.Tables["tableFile"]; DataRow dr = dt.NewRow(); dr["object"] = ms.GetBuffer(); //object is binary or varbinary // and GetBuffer() returns a byte[] dt.Rows.Add(dbRow); SqlCommandBuilder scb = new SqlCommandBuilder(sda); sda.Update(ds, "tableFile"); The instruction: dr["object"] = ms.GetBuffer(); throws the message exception "String or binary data would be truncated" could you help me, please? thanks in advance.

    P 1 Reply Last reply
    0
    • P pelos

      hello forum, i want to store c# objects in a MS SQL database. I think the best way is to serialize the object to a byte array but i have problems to insert a c# byte array into the database... First, the equivalent type of datum in MS SQL is the binary or varbinary type, isn´t it? well, my code: MyObject mo = new MyObject(); MemoryStream ms= new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); b.Serialize(ms,mo); SqlConnection sc = new SqlConnection("server=localhost;uid=sa;pwd=;database=myDB"); sc.Open(); SqlDataAdapter sda = new SqlDataAdapter("select * from myTable",sc); DataSet ds = new DataSet(); ad.Fill(ds,"tableFile"); DataTable dt = ds.Tables["tableFile"]; DataRow dr = dt.NewRow(); dr["object"] = ms.GetBuffer(); //object is binary or varbinary // and GetBuffer() returns a byte[] dt.Rows.Add(dbRow); SqlCommandBuilder scb = new SqlCommandBuilder(sda); sda.Update(ds, "tableFile"); The instruction: dr["object"] = ms.GetBuffer(); throws the message exception "String or binary data would be truncated" could you help me, please? thanks in advance.

      P Offline
      P Offline
      partyganger
      wrote on last edited by
      #2

      If the error says the string will be truncated, the size of the field you want to store it in, is to small.

      P 1 Reply Last reply
      0
      • P partyganger

        If the error says the string will be truncated, the size of the field you want to store it in, is to small.

        P Offline
        P Offline
        pelos
        wrote on last edited by
        #3

        ok. thank you. that was the problem! but ... another question: if the maxium size of a varbinary is 8000, how could i store an object with a greater lenght (for example, a file)? thank you. bye.

        A P 2 Replies Last reply
        0
        • P pelos

          ok. thank you. that was the problem! but ... another question: if the maxium size of a varbinary is 8000, how could i store an object with a greater lenght (for example, a file)? thank you. bye.

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          By using "blob" as the field's type: http://www.winnetmag.com/SQLServer/Article/ArticleID/20461/SQLServer\_20461.html

          1 Reply Last reply
          0
          • P pelos

            ok. thank you. that was the problem! but ... another question: if the maxium size of a varbinary is 8000, how could i store an object with a greater lenght (for example, a file)? thank you. bye.

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

            By using "blob" as the field's type: http://www.winnetmag.com/SQLServer/Article/ArticleID/20461/SQLServer\_20461.html Even better, just save a reference to the file i.e. save it's path as a string in the database. If you have a file server to store your files on, just have your frontend app copy the images there and then save the path (//FILESERVER/SQLSERVERFILES/file1.jpg) in the database. Although SQL server has better performance serving up images (streamed), if a lot of people start using the app at once, the server might get stuck at just streaming files all the time. By just saving the reference, you place the task of getting the image at the client.

            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