BLOBS and Dictionary<>

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1166 Views - Last Post: 11 October 2013 - 09:21 AM Rate Topic: -----

#1 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

BLOBS and Dictionary<>

Posted 10 October 2013 - 08:58 AM

I have a database insert function that uses a Dictionary<string, string>.

One of the DB fields is a password, which I am have stored in plain text. Now I want to convert it from plain text, by encrypting it and storing it as a BLOB in the DB. So, of course my insert function no longer works, since a BLOB is not a String. I looked it up, and Dictionary vals can only be Strings or Ints.

How would you handle this? Is there a collection that can store different datatypes? Or should I use a struct?

EDIT: I just found an example inthis tutorial by Andrewsw where he used an instance of a class as a Dictionary val, so I was incorrect in thinking that they only used Strings and Ints.

Maybe the real problem is that the password field is the only one that is not a string, or converted to a string. If I convert a BLOB to a string, doesn't that defeat the purpose of encrypting it? Can I have a collection with 5 strings and 1 BLOB?

This post has been edited by synlight: 10 October 2013 - 09:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: BLOBS and Dictionary<>

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9258
  • View blog
  • Posts: 34,735
  • Joined: 12-June 08

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 09:11 AM

FYI - the blob datatype (by itself) isn't encryption. Typically doing a hash (with a medium sized salt) works well.
Was This Post Helpful? 1
  • +
  • -

#3 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 09:17 AM

Thanks modi! Yes, I am doing a hash, I am using JackOfAllTrades tutorial to guide me.

http://www.dreaminco...sword-handling/

But, since the tutorial stops after hashing (I can't find the second one, I'm sure he's just been too busy to write it), I am trying to figure the rest out on my own. Since the hashed password is returned as a byte[], I figured a BLOB would be the way to go.

EDIT: I even considered leaving them as plain text, since this application only runs on a local machine. But that is a Very Bad Thing, and I may as well use this to learn how to do it.

This post has been edited by synlight: 10 October 2013 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,703
  • Joined: 16-October 07

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 09:33 AM

Read farther down, to CreateTextString and CreateByteArray in the offered code. You don't need a blob.
Was This Post Helpful? 2
  • +
  • -

#5 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 10:15 AM

Aannd now I feel dumb.

Thanks for pointing that out.. I can't believe I missed it.

But, I am curious now, what would one use when they had a collection with varying data types? In school we used a struct, by my professor was very old school and used a lot of deprecated stuff,
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,703
  • Joined: 16-October 07

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 10:47 AM

In C# you use a class. In databases you use tables.

A BLOB type, in any DB, is usually a bad hack. They don't behave well and basically degrade your DB into a third rate file system.

An object in C# can be decomposed into a row in a table, or several tables. Ideally, if you wished to use a database as an object store, the object in question would have some kind of "primary key" characteristics.
Was This Post Helpful? 2
  • +
  • -

#7 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 10:53 AM

So there is nothing in Systems.Collections namespace that can hold multiple datatypes? I'm trying to dig through MSDN and see, but there are a ton of members.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 11:56 AM

What do mean by hold multiple datatypes?
Was This Post Helpful? 0
  • +
  • -

#9 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 12:15 PM

Well, take for instance my user class. It consists of:

Int
String
String
String
String
String
Byte[]

My original problem has been solved, since Baav was kind enough to point out what I missed in the tutorial. But, my original intent was to change the Dictionary I was using to store my class instance into some sort of Collection that could hold the class attributes. But I haven't come across one that would allow me to do something like that. At this point, I'm just curious, probably file it away in my brain for future use.
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 12:30 PM

What's preventing you from defining your own class with those types as properties.
For example
public class Carton
{
  private Int32 _H = 0;
  private Int32 _W = 0;
  private Int32 _D = 0;
  private String _Text = "";
  private Double _Kgs = 0.0;
  
  public Carton(Int Width, Int32 Height, Int32 Depth, Double Kgs, String Text)
  {
    _W = Width;
    _H = Height;
    _D = Depth;
    _Kgs = Kgs;
    _Text = Text;
  }
  public readonly property Int32 Text { get: { return _Text; }}
  public readonly property Int32 Width { get: { return _W; }}
  public readonly property Int32 Height{ get: { return _H; }}
  public readonly property Int32 Depth{ get: { return _D; }}
  public readonly property Double Kgs{ get: { return _Kgs; }}
  public readonly property String Text { get: { return _Text; }}

  public Int32 Volume() { return _H * _D * _W; }

}



VB.net Tutorial: Parallel Arrays, Array with Object (Class & Structure)

This post has been edited by AdamSpeight2008: 10 October 2013 - 12:32 PM

Was This Post Helpful? 0
  • +
  • -

#11 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 12:38 PM

Nothing is preventing me from doing that. In fact, I do have a class defined. But, when I posted the OP, I was trying to pass that class to a general Insert function that used a Dictionary to add a record to a database. So, here is the code I was using to take fields from a registration form, instantiate them as a user, and then add them to the Dictionary that needed to be passed to the DataManager.Insert function..

//INSTANTIATE USER
                User newUser = new User();
                newUser.userFName = txtFName.Text;
                newUser.userLName = txtLName.Text;
                newUser.userEmail = txtEmail.Text;
                newUser.userUserName = txtUsername.Text;
                newUser.userPassword = password; //This field was a byte[] when I asked the original

                DataManager db = new DataManager();

                //Add to database
                Dictionary<String, String> data = new Dictionary<String, String>();
                data.Add("userFName", newUser.userFName);
                data.Add("userLName", newUser.userLName);
                data.Add("userEmail", newUser.userEmail);
                data.Add("userUsername", newUser.userUserName);
                data.Add("userPassword", newUser.userPassword);

                try
                {
                    //attempt database insert of new user
                    goodInsert = db.Insert("Users", data);
                }




.Aaaand now that I posted that, I'm wondering if I should have just taken the fields and put them directly in the Dictionary after validation. I guess it just seemed like bad form to me at the time?

This post has been edited by synlight: 10 October 2013 - 12:39 PM

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,997
  • Joined: 12-December 12

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 02:10 PM

Quote

I have a database insert function that uses a Dictionary<string, string>.

Unusual.. why a Dictionary?

As mentioned in your OP it is possible to create:

Dictionary<string, User> dic = new Dictionary<string, User>();

but this doesn't help because your latest code indicates that the Dictionary holds only a single row (User) each time. (There seems little point in creating a Dictionary to hold a single representation of an entity.)

Just my opinion but if you don't have to use a Dictionary then I wouldn't.

This post has been edited by andrewsw: 10 October 2013 - 02:22 PM

Was This Post Helpful? 1
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,997
  • Joined: 12-December 12

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 02:21 PM

:offtopic: Off topic, but it is possible to use something other than string or int as a Dictionary-Key. The type (Class) for this key would have to implement the GetHashCode() and Equals() methods. SO discussion.

But @OP don't let this distract you, it is not directly relevant to your question.
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,703
  • Joined: 16-October 07

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 05:13 PM

Oh, crap.

Please tell me db.Insert doesn't look something like:
public void Insert(string tableName, Dictionary<String, String> fields) {
	var sql = string.Format("insert into {0}({1}) values ({2})",
		tableName, 
		string.Join(",", fields.Keys),
		string.Join(",", fields.Values.Select((s)=>{ return string.Format("'{0}'", s); }))
		);
	GetCmd(sql).ExecuteNonQuery();
}



I suspect it is...

This is the worst way to go. To give the benefit of the doubt, it might use AddParam, but would still be a bad idea. You can only use strings as values? Really?

Write some honest code or use some kind of popular entity framework.
Was This Post Helpful? 0
  • +
  • -

#15 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: BLOBS and Dictionary<>

Posted 10 October 2013 - 05:43 PM

Andrew, no worries about distracting me. I do plenty of that on my own. The Insert function was actually modelled by a tutorial here on DIC, that used a Dictionary.

C# with SQLite

But now that I know more, I intend to rewrite the entire class. I'm probably just going to bite the bullet and switch over to SQLServer with Entity Framework. I have a couple of weeks until my test devices come in. Encryption and a SQL rework are the 2 things I had planned to work on while I wait.

And. Curtis posted a link to a tutorial about Parameterizing Queries a couple weeks ago that I intend to use to model proper queries.

Baav. Dude. I have mad respect for you. You've helped me a lot over my 2 years on this forum. But that was harsh. I assure you, I have written plenty of honest code. Considering I picked up C# and SQLite for the first time about a month ago, I don't think I'm doing too poorly. I know I have a lot of learning to do. ANd if I had my source code at home, I would post it for you to pick apart. But you can find an example in the tutorial I listed above. I did change it a bit, but on the whole it looks like that.

Now, as for why I thought you could only use strings and ints and values, well, I was trying to research Dictionary and came across a line somewhere that said that. But, I am on MSDN and tutorials 2 dozen times a day, so who knows where I saw that, and it certainly wasn't true. I didn't know that when I started the OP, but I certainly do now.

This post has been edited by synlight: 10 October 2013 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2