473,785 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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 2314
debasisdas
8,127 Recognized Expert Expert
As you have posted a question in the Article section it is being moved to SQL Server Forum

MODERATOR
Jul 21 '07 #2
jenniamia
5 New Member
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
13928
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 something like: char chars = "Hello!"; byte bytes = (byte) chars;
4
10310
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 bytes that are not printable characters? I've tested this and it seems to work fine, but I want to make sure there isn't some condition or situation I'm not aware of that could cause problems. I'm doing this because it's easier to do some of my...
5
4808
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
5401
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 "System.Text.Encoding.ASCII.GetString(bytearray)" of .Net library, we found that the char (delimiters) specified above are replaced with different char.
4
13410
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 be read and the string representation of the byte array has to be converted back to the original byte array and converted back to the original string - confused yet? in psuedo
2
3594
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 that is displayed in a text box. Once displayed, the text will be copied, transmitted and then pasted (all manually by humans) into a second utility where the string must then be reverse-engineered into the *original* byte array. The byte array will...
16
5450
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 learning all help would be greatly apperciated. Attribute VB_Name = "Module1" Option Explicit
12
2692
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 say. i got a couple of questions though: 1- is there any equivalent to java's Stream classes(ByteArrayInputStream,InputStreamRedare,DataInputStream ..) 2- how to open a stream on a byte ( char) array in c++ in order to read it as text. i have a...
24
2294
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
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10100
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.