2
I always worked attaching only images in my BDD tables, I remember that converted to MemoryStream
and then to Byte
and inserted... But I need to insert any kind of file extension in my database.
2
I always worked attaching only images in my BDD tables, I remember that converted to MemoryStream
and then to Byte
and inserted... But I need to insert any kind of file extension in my database.
2
Safe way to do what you want:
public static int databaseFilePut(MemoryStream fileToPut) {
int varID = 0;
byte[] file = fileToPut.ToArray();
const string preparedCommand = @"
INSERT INTO [dbo].[Raporty]
([RaportPlik])
VALUES
(@File)
SELECT [RaportID] FROM [dbo].[Raporty]
WHERE [RaportID] = SCOPE_IDENTITY()
";
using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
using (var sqlWrite = new SqlCommand(preparedCommand, varConnection)) {
sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
using (var sqlWriteQuery = sqlWrite.ExecuteReader())
while (sqlWriteQuery != null && sqlWriteQuery.Read()) {
varID = sqlWriteQuery["RaportID"] is int ? (int) sqlWriteQuery["RaportID"] : 0;
}
}
return varID;
}
I put in the Github for future reference.
Withdrawn of that response in the OS.
You can simplify some things if you don’t need all the features but you haven’t shown your specific need.
You just won’t take the using
and leave the code vulnerable to resource leakage.
Note the use of type VarBinary
for the SQL Server column.
Varbinary(MAX), right? but I didn’t understand the statement "safe mode"
Can it only be varbinary in the column? Or in addition to being varbinary you have to make the database receive filestream?
My wish is to receive Filestream, in case I pass only as byte is not possible to extract this file from the database right?
@pedro ideally yes, otherwise the file should be very small. What do you call extracting from the bank? I’ve never worked with FILESTREAM
then I wouldn’t know about it. Your question doesn’t talk about it. I believe that just specify this in the column and SQL Server will leave the file separate. Then I guess you have to test it to see if it solves what you need. See also: https://www.simple-talk.com/sql/learn-sql-server/an-introduction-to-sql-server-filestream/
@Bigown I referred to get the file and save on my disk (extract) rsrs, there is no question why I just discovered this... I will test your solution logically reading it seems to work! Thank you.
You are opening three resources. They need to be closed. Using the using
is used, there is the guarantee that the resources will be terminated and does not leave it "hanging" in the application even if an exception or other deviation occurs outside of the planned. This is a common error in programming. When a problem occurs, the programmer is lost without knowing what is happening and blames everything but the wrong program. Want to know more? You have some information here on website: http://answall.com/q/16065/101 and http://answall.com/q/22284/101. If you still do not understand, please open a question about this.
Man, thank you so much, I used the original topic tip and it worked here. Vlw even
Browser other questions tagged c# sql .net database winforms
You are not signed in. Login or sign up in order to post.
Just as you insert image you can insert any type of file. Show what you already have and what problems you are facing.
– Caffé
@Caffé I did not test because my field in my table of images is of type "Image" so it would not work in the same way... which type of field in the bank would have to be?
– Pedro
You can use the type
varbinary
.– Caffé
This does not solve, I want to insert the filestream to the bank and also extract. @Caffé
– Pedro
Then supplement your question, describe the requirements, show approximate examples of how you want the code... Otherwise whoever is interested in helping has to guess what you need.
– Caffé
All except you did not require me any more details and could answer me... I am not to blame if your interpretation is weak. @Caffé
– Pedro
Yes, and because you were very clear they left a lot of good answers and they all responded very well exactly what you need, right?
– Caffé
@Caffé More or less do not want to keep arguing, thanks for the attention... I will try the solutions that provided me.
– Pedro