473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Need help retrieving a byte array from SQL 2000 and converting it to a string

I need to make the string "myFile" into a byte[] and then put it in the database as a file/image, where later it needs to be pulled out and back into a string from the byte[]. I'm really having trouble gettting it back into a string afterwards, but I don't even know if the data is being put in the database with correct values. Any suggestions or pointers would be much appreciated. Thanks!

(I posted the main section I'm having trouble with, but I can post more or something different if it's needed.)

Expand|Select|Wrap|Line Numbers
  1.  
  2.                 FileStream fStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  3.                 BinaryWriter bw = new BinaryWriter(fStream);
  4.                 bw.Write(myFile);
  5.  
  6.                 //Create byte[] from string "myFile"
  7.                 int length = myFile.Length;
  8.                 byte[] xmlDataArray = new byte[length];
  9.                 fStream.Read(xmlDataArray, 0, length);
  10.  
  11.                 //Upload byte[] xmlDataArray to database
  12.                 conn.Open();
  13.                 SqlCommand cmd = new SqlCommand("INSERT INTO FileList (SetID, XMLFile) VALUES (@SetID, @XMLFile)", conn);
  14.                 cmd.Parameters.Clear();
  15.                 cmd.Parameters.AddWithValue("SetID", _SetID).SqlDbType = SqlDbType.Int;
  16.                 cmd.Parameters.AddWithValue("XMLFile", xmlDataArray).SqlDbType = SqlDbType.Image;
  17.                 cmd.ExecuteNonQuery();
  18.  
  19.                 //----------------------------------------------------------------------------------
  20.                 //Retrieve stored xmlDataArray from database
  21.                 SqlCommand cmmd = new SqlCommand("SELECT XMLFile FROM FileList WHERE SetID=@SetID", conn);
  22.                 cmmd.Parameters.Clear();
  23.                 cmmd.Parameters.AddWithValue("SetID", _SetID).SqlDbType = SqlDbType.Int;
  24.                 byte[] xmlDataArrayOut = (byte[])cmmd.ExecuteScalar();
  25.  
  26.                 //Convert byte[] back to string
  27.                 ASCIIEncoding encoding = new ASCIIEncoding();
  28.                 string constructedString = encoding.GetString(xmlDataArrayOut);
  29.  
Jul 19 '07 #1
2 2294
debasisdas
8,127 Expert 4TB
As you have posted a question in the Article section it is being moved to SQL Server Forum

MODERATOR
Jul 21 '07 #2
Just in case anyone else made this mistake, I used encoding to get it into a byte array and later used encoding to get it out. Anyway, seems like that should've been obvious before, but this was what worked for me.
Expand|Select|Wrap|Line Numbers
  1. FileStream fileStream = new FileStream(toolFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  2. StreamReader streamReader = new StreamReader(fileStream);
  3. myFile = streamReader.ReadToEnd();
  4.  
  5. //Create byte[] from string "myFile"
  6. int length = myFile.Length;
  7. byte[] xmlDataArray = new byte[length];
  8.  
  9. System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  10. xmlDataArray = encoding.GetBytes(myFile);
  11.  
  12. //Upload byte[] xmlDataArray to database
  13. conn.Open();
  14. SqlCommand cmd = new SqlCommand("INSERT INTO FileList (SetID, XMLFile) VALUES (@SetID, @XMLFile)", conn);
  15. cmd.Parameters.Clear();
  16. cmd.Parameters.AddWithValue("SetID", _SetID).SqlDbType = SqlDbType.Int;
  17. cmd.Parameters.AddWithValue("XMLFile", xmlDataArray).SqlDbType = SqlDbType.Image;
  18. cmd.ExecuteNonQuery();
  19.  
  20. //----------------------------------------------------------------------------------
  21. //Retrieve stored xmlDataArray from database
  22. SqlCommand cmmd = new SqlCommand("SELECT XMLFile FROM FileList WHERE SetID=@SetID", conn);
  23. cmmd.Parameters.Clear();
  24. cmmd.Parameters.AddWithValue("SetID", _SetID).SqlDbType = SqlDbType.Int;
  25. byte[] xmlDataArrayOut = (byte[])cmmd.ExecuteScalar();
  26.  
  27. //Convert byte[] back to string
  28. ASCIIEncoding encoding = new ASCIIEncoding();
  29. string constructedString = encoding.GetString(xmlDataArrayOut);
Jul 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
5
by: jackie | last post by:
public static String toGBString( String asciiStr ) { String s = ""; try { rtnStr = new String( ascii.getBytes(), "gb2312" ); } catch( Exception ex ) {} return s; }
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
12
by: steven acer | last post by:
hello, i have a java app that constructs an xml from a specific file format and vice versa. i've been asked to convert it to c++, but im not an expert in c++, actually im mere beginner you can...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.