First of all I'm using a FileStream
with the server. The server is SQL Server express 2014.
I configured the database and table correctly (I hope) and I was able to upload an image as a varbinary
, but when I try to download that image I get an error
An invalid parameter was passed to the function.
Here is the database structure
Records(
[id] [uniqueidentifier] ROWGUIDCOL NOT NULL UNIQUE,
[Name] [varchar](64) NULL,
[Clip] [varbinary](max) FILESTREAM NULL,
)
And the code for downloading the image
private object GetTransactionContext()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()";
cmd.CommandType = CommandType.Text;
cmd.Connection = sql_Connection;
return cmd.ExecuteScalar();
}
private void BeginTransaction()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "BEGIN TRANSACTION";
cmd.CommandType = CommandType.Text;
cmd.Connection = sql_Connection;
cmd.ExecuteScalar();
}
private void CommitTransaction()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "COMMIT TRANSACTION";
cmd.CommandType = CommandType.Text;
cmd.Connection = sql_Connection;
cmd.ExecuteScalar();
}
//Command for reading the data
public async void ReadFromDatabase(string Name)
{
//enter the command parameter
sql_Command_Read.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
//open a connection to the server
sql_Connection.Open();
this.BeginTransaction();
//get the path to the BLOB object
string filePath = null;
Object pathObj = sql_Command_Read.ExecuteScalar();
if (DBNull.Value != pathObj)
{
filePath = (string)pathObj;
}
else
{
throw new NotImplementedException();
}
Object obj = this.GetTransactionContext();
byte[] sql_TransactionToken = (byte[])obj;
sql_FileStream = new SqlFileStream(filePath, sql_TransactionToken, FileAccess.ReadWrite, FileOptions.SequentialScan, 0);
byte[] buffer = new byte[(int)sql_FileStream.Length];
sql_FileStream.Seek(0L, SeekOrigin.Begin);
sql_FileStream.Read(buffer, 0, buffer.Length);
File.WriteAllBytes("C:\test.mp4", buffer);
this.CommitTransaction();
}
The content of the command is
sql_Command_Read.CommandText = "SELECT Picture.PathName() FROM Archive.dbo.Records WHERE Name = @Name";
Again I am new to database and sql client programming. Now about the code. The error that I get is on this line:
sql_FileStream = new SqlFileStream(filePath, sql_TransactionToken, FileAccess.ReadWrite, FileOptions.SequentialScan, 0);
and about the parameters here are the values they get:
filePath = "\\\\LAPTOP-PC\\VIDEOPRESENTERDB\\v02-A60EC2F8-2B24-11DF-9CC3-AF2E56D89593\\Archive\\dbo\\Records\\Picture\\C94D4189-9ECF-448B-B05A-ABF9331BF6CE\\VolumeHint-HarddiskVolume2"
obj has 16 numbers ranging from 0 to 255.
I am obviously making a mistake somewhere but I don't know where exactly