B
B
beduin012016-04-14 11:37:02
C++ / C#
beduin01, 2016-04-14 11:37:02

Why is Stream needed in C#?

It took me here in C # to write a tiny application. The task is to read a blob from one database and write it to another (also a blob).
C# I know only superficially. I don't understand this moment. Why are some Streams used in all the examples?

byte[] userBlob;

                myCommand.CommandText = "SELECT id, userblob FROM USERS";
                myCommand.Connection = myFBConnection;
                myCommand.Transaction = myTransaction;

                FbDataReader reader = myCommand.ExecuteReader();
                
                try
                {
                    while(reader.Read())
                    {
                        Console.WriteLine(reader.GetString(0));
                        userBlob = // Вот тут что должно быть? Читать как биты или поток

                    }
                }

                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Can't read data from DB");
                }

DB. firebird.
It seems that I need the `reader.GetBytes` method, right?
Tried like this:
userBlob = reader.GetBytes(0,0,userBlob,0,userBlob.Length);

But I can’t understand why it is necessary to pass userBlob to the method parameters again if data is read into it.
And by the way, it swears: Cannot convert type long to byte[]
here, as I understand it, the size of the buffer is supposed to be specified - the question is, how to find out how much data will be in the blob itself?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2016-04-14
@beduin01

GetBytes returns a long, I don't know where you found that it returns an array of bytes:
https://msdn.microsoft.com/en-us/library/system.da...
So passing a buffer as the third parameter is more than logical.
Come on?) Didn't that make you think about the type of return value?) Did you come up with it yourself that it returns an array of bytes?
Well, if you still go through the documentation, then below you will see the following:

GetBytes returns the number of bytes available in the field. This value is often equal to the exact length of the field. However, the number returned may be less than the actual length of the field if GetBytes has already been used to get bytes from the field.
When passing a buffer whose value is null, the GetBytes method returns the length of the string in bytes.

Therefore, if you don't know the length of the buffer - call the method with null to get this length, create a buffer and put the value there with the second call to GetBytes.
In .net and C#, the documentation is normal, it is useful to read it.
Because they are BLOBs and BLOBs for Large Object. A blob can be kilobyte or gigabyte in size. It is far from always advisable to read a gigabyte into an entire array; as a rule, it is not advisable at all.
Streams are an abstraction in the standard library that implements the concept of stream processing. The file stream will not load the entire 10 gig file into memory if you only read the first kilobyte. With blobs from the database, the situation is similar. Even if your particular database driver does not support loading a blob in parts, in principle this practice exists.

M
MADm, 2016-04-14
@MADm

Apparently you are using Firebird. I can't figure out which library is being used.
Have you looked here? codeproject I can assume that you need to get data like this userBlob = reader[column number];
But I advise you to better read the documentation for the specific library that you are using, or use another library for your database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question