C#.NET Blob/file into ultralite

I have been tasked w/ porting some C++ to C#.NET.  I am having an issue 
w/ inserting a file into the db.  Just a simple text file into a long 
binary field.

I can't seem to figure out how to assign the parameter (streamwriter I 
am assuming) that puts the data into the field.

So, I have this:

             using (ULCommand cmd = conn.CreateCommand())
             {
                 cmd.CommandText = "INSERT INTO..."
         ...
         StreamWriter sw = <not sure>; <--- ????

         using (FileStream fs = File.Open(fSFile, FileMode.Open))
                 {

                         Console.WriteLine("File exists - using");
                         byte[] b = new byte[FILE_BUFFER_SIZE];
                         UTF8Encoding temp = new UTF8Encoding(true);
                         while (bytes_read == FILE_BUFFER_SIZE)
                         {
                             bytes_read = fs.Read(b, 0, b.Length);
                             sw.Write(temp.GetString(b));
                         } //while
                 } //using

                 cmd.Parameters.Add("", sw);  <--- ????

         ...

What am I missing here?  Do I not need the streamwriter in this case? Do 
I just insert the bytes (but then, how do you know how many bytes there 
are)?
0
Scott
12/15/2010 1:56:43 PM
sybase.sqlanywhere.ultralite 2982 articles. 0 followers. Follow

2 Replies
721 Views

Similar Articles

[PageSpeed] 54

Scott,

I haven't tested this, but you could give it a try:

A function for getting the file contents:

     private byte[] GetFileContents(String filepath) {
         FileStream stream = new FileStream(filepath, FileMode.Open, 
FileAccess.Read);
         BinaryReader reader = new BinaryReader(stream);

         byte[] ret = reader.ReadBytes((int)stream.Length);

         reader.Close();
         stream.Close();

         return ret;
     }

And for setting it in parameters:

ULParameter file_param = new 
ULParameter("FileContents",ULDbType.LongBinary); //or change to varchar, 
whatever fits the database
file_param.Value = GetFileContents(filepath);

HTH
-Tyson

On 12/15/2010 6:56 AM, Scott Trafford wrote:
> I have been tasked w/ porting some C++ to C#.NET.  I am having an issue
> w/ inserting a file into the db. Just a simple text file into a long
> binary field.
>
> I can't seem to figure out how to assign the parameter (streamwriter I
> am assuming) that puts the data into the field.
>
> So, I have this:
>
> using (ULCommand cmd = conn.CreateCommand())
> {
> cmd.CommandText = "INSERT INTO..."
> ...
> StreamWriter sw = <not sure>; <--- ????
>
> using (FileStream fs = File.Open(fSFile, FileMode.Open))
> {
>
> Console.WriteLine("File exists - using");
> byte[] b = new byte[FILE_BUFFER_SIZE];
> UTF8Encoding temp = new UTF8Encoding(true);
> while (bytes_read == FILE_BUFFER_SIZE)
> {
> bytes_read = fs.Read(b, 0, b.Length);
> sw.Write(temp.GetString(b));
> } //while
> } //using
>
> cmd.Parameters.Add("", sw); <--- ????
>
> ...
>
> What am I missing here? Do I not need the streamwriter in this case? Do
> I just insert the bytes (but then, how do you know how many bytes there
> are)?

0
Tyson
12/18/2010 12:45:39 AM
Tyson

Great!  Works like a charm.

Thanks,
Scott

On 17/12/2010 7:45 PM, Tyson Lewis wrote:
> Scott,
>
> I haven't tested this, but you could give it a try:
>
> A function for getting the file contents:
>
> private byte[] GetFileContents(String filepath) {
> FileStream stream = new FileStream(filepath, FileMode.Open,
> FileAccess.Read);
> BinaryReader reader = new BinaryReader(stream);
>
> byte[] ret = reader.ReadBytes((int)stream.Length);
>
> reader.Close();
> stream.Close();
>
> return ret;
> }
>
> And for setting it in parameters:
>
> ULParameter file_param = new
> ULParameter("FileContents",ULDbType.LongBinary); //or change to varchar,
> whatever fits the database
> file_param.Value = GetFileContents(filepath);
>
> HTH
> -Tyson
>
> On 12/15/2010 6:56 AM, Scott Trafford wrote:
>> I have been tasked w/ porting some C++ to C#.NET. I am having an issue
>> w/ inserting a file into the db. Just a simple text file into a long
>> binary field.
>>
>> I can't seem to figure out how to assign the parameter (streamwriter I
>> am assuming) that puts the data into the field.
>>
>> So, I have this:
>>
>> using (ULCommand cmd = conn.CreateCommand())
>> {
>> cmd.CommandText = "INSERT INTO..."
>> ...
>> StreamWriter sw = <not sure>; <--- ????
>>
>> using (FileStream fs = File.Open(fSFile, FileMode.Open))
>> {
>>
>> Console.WriteLine("File exists - using");
>> byte[] b = new byte[FILE_BUFFER_SIZE];
>> UTF8Encoding temp = new UTF8Encoding(true);
>> while (bytes_read == FILE_BUFFER_SIZE)
>> {
>> bytes_read = fs.Read(b, 0, b.Length);
>> sw.Write(temp.GetString(b));
>> } //while
>> } //using
>>
>> cmd.Parameters.Add("", sw); <--- ????
>>
>> ...
>>
>> What am I missing here? Do I not need the streamwriter in this case? Do
>> I just insert the bytes (but then, how do you know how many bytes there
>> are)?
>

0
Scott
12/23/2010 9:58:32 PM
Reply:

Web resources about - C#.NET Blob/file into ultralite - sybase.sqlanywhere.ultralite

NEC UltraLite - Wikipedia, the free encyclopedia
The product was originally developed by an NEC Japan telecommunications engineering team that was trying to make an inexpensive lightweight terminal ...

1992 GM Ultralite running concept car - YouTube
... the North American Auto Show in Detroit in January 1992, the centerpeace of the General Motors exhibit, the running fully functional GM Ultralite ...

The GM Ultralite — 1400 lbs, 100 MPG, and You Can’t Have It
... and far less well known than it should have been, and we all taste bitter regret at what could have been . This is the case with the GM Ultralite, ...

NEC debuts VersaPro UltraLite VB laptop, Mate MG all-in-one for Japan
Like your computers to be all-business and only available in Japan? Then you might want to take a gander at NEC's latest duo, which includes ...

New Freescale I.MX6 SoCs Include IoT-focused UltraLite
... including new DualPlus and QuadPlus parts featuring enhanced GPUs and expanded memory support, and a new low-end, IoT focused 528MHz UltraLite ...

Portlandia Meets Ad Agency Snobbery This one hits... - 2matts.com
Portlandia Meets Ad Agency Snobbery This one hits a little too close to home. [via: IFC]

Travelers Have Always Expected Suitcases to be Light and Strong - Adweek
Travelers have always expected suitcases to be light and strong

NEC – New business-oriented PC lineup – 12 types (28 models)
... 34.9mm slim body 15.6 inch laptop PC “Versa Pro Type VX” with USB 3.0 built-in and a super light (875g, 14.9mm thin) laptop PC “VersaPro UltraLite ...

BASE Jumping Articles
Skip to Content Basejumper.com Home Forums Articles Gear Classifieds Photos Videos Email Help Submit an Article My Bookmarks Tags Search Forums ...

New Technology Running Shoes : Reebok RealFlex Run Men's
New technology running shoes like these Reebok RealFlex Run Men's "Give your feet the flexibility they deserve. RealFlex sensors work together ...

Resources last updated: 2/25/2016 1:41:42 AM