Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Edited.... Since I am writing this from my phone I want to make sure it's clear enough. All I really am requesting is what is the best way to keep the integrity of formatted text into a database then when I do a select statement it keeps the same format as it was before I updated the database. Would encrypting it or hashing it do the trick?



What would be the best way to hold large amount of formatted text in a database without losing the text format?

What I want to do is store code (like copy all code from a .CS file) and save it in a database. Then I want to do a select statement and write it to a text file where the format is still the same.


For example....if I have....

[code]

using System;  
using System.IO;  
  
class TexFileSample  
{  
    public static void Main()   
    {  
          
        using (StreamWriter sw = new StreamWriter(@"C:\SomeTextFile.txt"))   
        {             
            sw.Write("Writing Text ");  
            sw.WriteLine("Here we go.");  
            sw.WriteLine("-------------------");  
            sw.Write("Today is is: " + DateTime.Now);  
            sw.WriteLine("Done");  
        }  
    }  
}  

[/code]

And I want to insert this into a database I can pretty much copy and paste into a table, but that doesn't seem like the best logical way. So when I do a select statement and bring it back out I want the code block to look exactly as it was before going into the database.

I was thinking it should be hashed or encrypted or something, but not sure what I should do.

Any suggestions?

What I have tried:

Read a lot and searched for best way.
Posted
Updated 21-Jan-19 12:18pm
v2
Comments
Graeme_Grant 20-Jan-19 22:55pm    
What have you tried so far?
Member 12719658 20-Jan-19 23:28pm    
Well, right now I am AES encrypting the text. Everything works fine, but not sure if i should keep it encrypted or would hashing it to hex or something would be better.

What do you think? The code blocks that I insert into SQLite can be pretty long (its actually text from my .cs files and could be text from java file also) so I do not know if there is a way to make sure to keep the integrity of the format more secure or would encrypting it be fine.

I do not care about security of the files really.
Wendelius 21-Jan-19 23:51pm    
Not sure why the question is downvoted. Compensated with an upvote.

If I understand the question correctly, you have two separate problems, formatting and ordering.

What comes to formatting, if the data contains plain ANSI or unicode characters, then you should be fine with TEXT[^] datatype in the database as long as you use SqliteParameter Class (Microsoft.Data.Sqlite) | Microsoft Docs[^] correctly and define unicode string for SqliteParameter.DbType Property (Microsoft.Data.Sqlite) | Microsoft Docs[^].

If the data contains something more exotic such as pictures in binary format, I'd say BLOB would be the only way.

Then about the ordering. A select basically returns the data in 'random' order unless an ORDER BY clause is used, see SQLite Query Language: SELECT[^]
So to ensure that the data (rows) is returned in the same order as they were read, you need to have for example an integer column which you would increment every time a row is inserted. In general an autoincrement column should suffice if you insert the data in the correct order, see SQLite Autoincrement[^]

Of course for all the things to work you need to ensure that no formatting or ordering is lost while reading the data so you need to make sure that you use proper reader to read the ansi, unicode or binary data, which ever is the case.
 
Share this answer
 
Comments
Member 12719658 21-Jan-19 18:18pm    
Well, that had nothing to do with my question. It was asking if I should update database with formatted text or encrypt it / hash. Nothing to do about ORDER BY or AUTOINCREMENTS or DataTypes. I think your response was auto generated or something so no points for you.
Wendelius 21-Jan-19 23:50pm    
Sorry to hear that but that was how I understood the question, how to input and output the text to and from the database so that the text doesn't change during the process even if the data is split into multiple rows in the table.

Thanks for giving the feedback. I appreciate it since English isn't my native language so these are always good chances to learn.
Member 12719658 22-Jan-19 9:24am    
I apologize if I came across wrong. I edited my question previously since I was on my phone writing it. As per my edit, I was just wanting to know what would be the best way to store data in a database without losing the integrity of the formatted data from a C# program. I have noticed before by updating or inserting data from a string - character returns and spacing have changed and I wanted to make sure I didn't lose them. So I asked if encrypting or hashing would be better. However, I provided a block of code as an example of what would be stored and was only to show the format of the text. I didn't realize that compressing and decompressing would do the trick.

I apologize if I was not clear enough and will give you stars for your help. I just could not see where Ordering came into play, however, I may have wrote a question that was not understandable.

Thanks for your help.
Wendelius 26-Jan-19 3:23am    
No need to apologize or re-rate at all :) I just wanted to explain that the answer wasn't auto generated, I simply misunderstood the question.
Found out about Compress and Decompress so I went with that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900