getting binary data out of the database

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 46
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Light Poster

getting binary data out of the database

 
0
  #1
16 Days Ago
I am doing an app which inclueds richTextBox, for inserting text. I put some text into richTextBox and save it into my sql database (column is varbinary(MAX) data type, with this code:
  1. byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);
Here the text is changed into binary data, and them I add parameters and all whats needed, and finally:
  1. cmd1.ExecuteNonQuery();
Then, when I want to open it, it opens in word (the code is made, that it opens in this program - depending on an extention and a file type).

So, when the Word is starting up, 1st it asks me if I want to change it (to use some other coding). I select nothing and click Ok. The text is there but it is not in the shape I inserted it into richTextBox. The font are all the same and are in Courier New (I didn`t save the the text in with this font, and even the text was not all the same).

Anyone has any clue how to get the same text out, as I saw inserted into richTextBox? Is this maybe anything to do with richTextBox, or this is only a matter of coding?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 91
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #2
15 Days Ago
Are you first saving the encoded text as a Word document?
If so, are you using any Office Automation for this or just saving bytes?

Have you thought about saving it as XML, then loading it in WORD?

You can get the WORD XML tags by saving a simple document from WORD as XML -- then extract out all of the XML shell and plug your text inside.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 46
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Light Poster
 
0
  #3
13 Days Ago
I only save vbytes:
  1. byte[] DobiDatoteko = File.ReadAllBytes(myFullPath);

Do you have any example of transforming the text (or bytes) into xml?
Last edited by Mitja Bonca; 13 Days Ago at 4:53 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
13 Days Ago
If you write the byte content of the RTF directly to disk (bypassing the SQL Server) and open it with word do you receive the same formatting message when opening the document? I suspect that you will. If you do this eliminates SQL as a culprit in this issue. It is probably Word complaining that you're asking it to open a non-native .docx file, or an RTF file it didn't create.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 46
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Light Poster
 
0
  #5
13 Days Ago
Originally Posted by sknake View Post
If you write the byte content of the RTF directly to disk (bypassing the SQL Server) and open it with word do you receive the same formatting message when opening the document? I suspect that you will. If you do this eliminates SQL as a culprit in this issue. It is probably Word complaining that you're asking it to open a non-native .docx file, or an RTF file it didn't create.
No. if I open it in the defualt program, or with word or with acrobat reader, the text IS the same as before.
I save it into the database with the :
  1. byte[] DobiDatoteko = File.ReadAllBytes(myFullPath);

I open the file with:
  1. byte[] buffer = (byte[])cmd1.ExecuteScalar();
  2. povezava.Close();
  3. FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MojaMapa\" + listView1.SelectedItems[0].Text, FileMode.Create); //, FileAccess.ReadWrite);
  4. fs.Write(buffer, 0, buffer.Length);
  5. fs.Close();
As said, this works without any problem.
But if I want to open it in a richTextbox with:
  1. byte[] buffer = (byte[])cmd1.ExecuteScalar();
  2. String myString = Encoding.ASCII.GetString(buffer);
I got those strange signs.
Maybe I can not transform the bytes to string... there is maybe the wrong transformation.
Last edited by Mitja Bonca; 13 Days Ago at 5:20 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #6
13 Days Ago
Impossible. From your earlier code example you showed us this:
  1. byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);

richTextBox1.Text returns the unformatting string of words in the RTF textbox. Now richTextBox1.Rtf will give you the formatted rich text. It is impossible that you saw the formatted rich text using the code provided earlier, something else is wrong.

Now this is how you can save an RTF control to a file:
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. //This works OK
  4. const string fName = @"C:\rtf.rtf";
  5. if (System.IO.File.Exists(fName))
  6. System.IO.File.Delete(fName);
  7. System.IO.File.WriteAllBytes(fName, Encoding.UTF8.GetBytes(richTextBox1.Rtf));
  8. System.Diagnostics.Process.Start("wordpad", "\"" + fName + "\"");
  9. }

And using this SQL table:
  1. IF OBJECT_ID('RTFStore', 'U') IS NOT NULL DROP TABLE RTFStore
  2. CREATE TABLE RTFStore
  3. (
  4. RecordId INT identity(1000, 1) PRIMARY KEY,
  5. RTF VARBINARY(max)
  6. )

You can store/retrieve RTF to a database using:
  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = "Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;";
  4. int rtfRecordId;
  5.  
  6. using (SqlConnection conn = new SqlConnection(connStr))
  7. {
  8. conn.Open();
  9. const string insQuery = "Insert Into RTFStore (RTF) Values (@RTF) Select CAST(SCOPE_IDENTITY() as int) As Result";
  10. using (SqlCommand cmd = new SqlCommand(insQuery, conn))
  11. {
  12. cmd.Parameters.Add(new SqlParameter("@RTF", SqlDbType.VarBinary)).Value = System.Text.ASCIIEncoding.UTF8.GetBytes(richTextBox1.Rtf);
  13. rtfRecordId = Convert.ToInt32(cmd.ExecuteScalar());
  14. }
  15. }
  16. using (SqlConnection conn = new SqlConnection(connStr))
  17. {
  18. conn.Open();
  19. const string selQuery = "Select RTF From RTFStore Where RecordId = @RecordId";
  20. using (SqlCommand cmd = new SqlCommand(selQuery, conn))
  21. {
  22. cmd.Parameters.Add(new SqlParameter("@RecordId", SqlDbType.Int)).Value = rtfRecordId;
  23. object oBuffer = cmd.ExecuteScalar();
  24. if (oBuffer != DBNull.Value)
  25. {
  26. byte[] buffer = (byte[])oBuffer;
  27. const string fName = @"C:\rtf2.rtf";
  28. File.WriteAllBytes(fName, buffer);
  29. System.Diagnostics.Process.Start("wordpad", "\"" + fName + "\"");
  30. }
  31. }
  32. }
  33. }

I'm not sure what is wrong with your original code since you haven't posted the complete sample but give this a shot.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 46
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Light Poster
 
0
  #7
13 Days Ago
Originally Posted by sknake View Post
Impossible. From your earlier code example you showed us this:
  1. byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);
No. I do not get the text form richTextBox. I get the bytes from database and I would like to put this bytes (of course before thatr I have to transform them to string (text) into richTextBox.

Do we understand each other now?


This is now I get the bytes out of the database:
  1. byte[] buffer = (byte[])cmd1.ExecuteScalar();

And this is how I transform bytes to string:
  1. String myString = Encoding.ASCII.GetString(buffer);

And this is my problem - the transformation doesn`t work correctly. I o not get the correct text into "myString", but just some strange signs (as said earlier).
Here the richTextBox has nothing to do with this, richTextBox comes after that.

I would like to know how to do the correct transformtion from byte aray to string. This is my problem.


EDIT: I just did this code which should suppose to do the transformation:
  1. byte[] buffer = (byte[])cmd1.ExecuteScalar();
  2. //String myString = Encoding.ASCII.GetString(buffer);
  3. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8; //or .ASCII, or what ever I try here
  4. String myString = myEncoding.GetString(buffer);

But again no luck - the coding is incorrect.
Last edited by Mitja Bonca; 13 Days Ago at 9:05 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #8
13 Days Ago
That depends on how the data was originally stored in to the SQL Server. Are you putting the text there? If so please post the code.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 46
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Light Poster
 
0
  #9
13 Days Ago
1.
This is for the files from the hdd:
  1. string myPath = treeView1.SelectedNode.FullPath;
  2. string myFileName = listView1.FocusedItem.Text;
  3. string myFullPath = Path.Combine(myPath, myFileName);
  4. byte[] DobiDatoteko = File.ReadAllBytes(myFullPath);
Is this OK?

2.
And this is how I save from a richTextBox - damn sorry you were right. Didnt even noticed that I save it this way. Iwill repair for the richTextBox.
  1. byte[] saveText = Encoding.UTF8.GetBytes(richTextBox1.Text);
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #10
13 Days Ago
Upload your project man. You haven't been giving me complete answers for this whole thread

Where is the code that inserts the byte array in to the sql server? where is the code that writes the byte[] buffer to a file on the disk.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC