none
How to convert byte[] to base64Binary (not base64String)? RRS feed

  • Question

  • The SQL server table column type is Image.

    The C# code converts a file (e.g. .docx) to byte[]. The byte[] can be insert into the the Image column in SQL. But the client required base64Binary to convert back to a file. They received an error "is not a valid value for 'base64Binary" and "Invalid length for a Base-64 char array". How can I convert the byte[] to base64Binary?

    I tried base64String. I got "nvarchar is incompatible with image". I google it but it seems only finding convert to base64String.

    Thanks.



    Tuesday, July 24, 2018 5:31 PM

All replies

  • I think that you should use Convert.ToBase64String not for inserting the data to database, but for converting the extracted bytes to base-64 string before sending the string to Clients. The Clients will use Convert.From64String.

    Show the details about the data and errors. Which strings are sent to Clients?


    Tuesday, July 24, 2018 7:04 PM
  • The client does not want string. They said their code works for other data that was sent from another database.
    Tuesday, July 24, 2018 7:14 PM
  • Hi friend,

    Welcome to the MSDN forum.

    Refer to your description, this issue is about the C# development, since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for a better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Wednesday, July 25, 2018 2:50 AM
  • Hi friend,

    Welcome to the MSDN forum.

    Refer to your description, this issue is about the C# development, since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for a better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    which forum you moved this issue to, I want to know what other C# development forum is there. Thanks
    Wednesday, July 25, 2018 4:19 AM
  • Hi danyeungw,

    Based on my test, Convert.From64String() could get what you want. Here is a simple example for your reference.

      public static void LoadFromDB()
            {
                SqlConnection con;
                SqlCommand cmd;
                con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + @"C:\Users\wendyz.WICRESOFT.000\OneDrive\Projects\VS2017 Project\WindowsForms\WindowsForms\Database.mdf;" + "Integrated Security=True");
                con.Open();
                cmd = new SqlCommand("select Image from Image where id=1", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                byte[] originalArray = (byte[])ds.Tables[0].Rows[0]["Image"];
                string temp_inBase64 = Convert.ToBase64String(originalArray);
    
                byte[] temp_backToBytes =Convert.FromBase64String(temp_inBase64);
                using (MemoryStream ms = new MemoryStream(temp_backToBytes))
                {
                    Bitmap bmp = new Bitmap(ms);
                    bmp.Save("SaveFromDB.jpg");
                }
    
            }

    If they do not want string, what is the base64Binary they want to convert to?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, July 25, 2018 7:45 AM
    Moderator
  • Actually, I convinced the client to use Base64String. And I tested the string works. Thanks.

    • Edited by danyeungw Wednesday, July 25, 2018 9:37 PM
    Wednesday, July 25, 2018 8:53 PM
  • Hi danyeungw,

    If your question has been solved, please mark the useful reply as answer. This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 26, 2018 1:42 AM
    Moderator