Results 1 to 11 of 11

Thread: C# and SQL types

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    C# and SQL types

    Hi there, this is a similar Q as my other thread but not the same...

    What I am wondering is, in SQL we can stores binary. And in C# we have to make byte[] object and then send that value stored in the byte[] paramater to SQL and its stored.

    but when RETRIEVING from SQL using dataReader, and that value to retrieve is of SQL binary.... what type should I use in order to retrieve the value?

    i.e:
    Code:
    object theDataReadFromSQL;
    
    while(dataReader.Read())
    {
       theDataReadFromSQL = dataReader.GetValue(0);
    }
    thats fine but the thing is, I cannot compare the bytes with the bytes of something else, as the bytes stored in theDataReadFromSQL is of type object.

    its hard to explain... but i hope you understand...?

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: C# and SQL types

    I've never had to use it but the DataReader has a GetBytes method. Would that not be more appropriate method than GetValue

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: C# and SQL types

    I remember looking at it however I do not know what parameters to give it....

    what should i give the parameters? I do not understand the last 3/4 of them...

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: C# and SQL types

    theres an example here of reading and writing blobs

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: C# and SQL types

    thanks, im just trying it but tryin to figure out how to compare the long bytes value given from SQL to the bytes calculated from the textbox on the form...

    [edit]
    Just tried that suggestion, its not giving me the thing i want, it gives me the length of the value retrieved. maybe you misunderstood what I said?
    [/edit]
    Last edited by Techno; Jun 24th, 2005 at 09:12 AM.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: C# and SQL types

    currently I have a way but the thing is im using object type to get the value from SQL using dataReader.GetValue(0) then compare that with the object value of bytes of the textbox - gets me the correct bytes but the one on the textbox is less than 50 chars... anythin from sql will be of length 50 chars.

    I dont want to have to do an unprofessional string concat stuff to make the value of length 50 chars

  7. #7
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: C# and SQL types

    Quote Originally Posted by Techno
    [edit]
    Just tried that suggestion, its not giving me the thing i want, it gives me the length of the value retrieved. maybe you misunderstood what I said?
    [/edit]
    byte outbyte[] = new byte[50];
    retval = myReader.GetBytes(1, 0, outbyte, 0, 50);

    retval stores the number of bytes read.
    outbyte stores the actual data.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: C# and SQL types

    excellent! thanks for this

    but now... to somehow make the bytes value contained in the textbox the same length as outbyte[]

  9. #9
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: C# and SQL types

    you could try an Array.Copy
    byte[] dest = new byte[50]
    Array.Copy(source,0,dest,0,source.Length);

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: C# and SQL types

    thanks

    looks good but when i do an if statement like this, its always returned as false!


    Code:
    if(dest == source)
    {
    }
    else
    {
    //always goes here!
    }
    but I have looked at both of the values and both are the same! Same result occurs if I do a .Equals()

  11. #11
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: C# and SQL types

    AFAIK == and Equals compare references unless the class overides them so that they explicitly perform a comparsion on the contents.
    I guess a loop is needed to check each item.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width