从sql server下载数据时“无效参数”

时间:2015-01-01 13:59:27

标签: c# sql-server filestream

首先,我在服务器上使用FileStream。服务器是SQL Server express 2014。 我正确地配置了数据库和表(我希望)并且我能够将图像上传为varbinary,但是当我尝试下载该图像时出现错误

  

将无效参数传递给函数。

这是数据库结构

Records(
    [id] [uniqueidentifier] ROWGUIDCOL  NOT NULL UNIQUE,
    [Name] [varchar](64) NULL,
    [Clip] [varbinary](max) FILESTREAM  NULL,
)

下载图片的代码

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();
 }

命令的内容是

sql_Command_Read.CommandText = "SELECT Picture.PathName() FROM Archive.dbo.Records WHERE Name = @Name";

同样,我是数据库和SQL客户端编程的新手。现在关于代码。我得到的错误就在这一行:

sql_FileStream = new SqlFileStream(filePath, sql_TransactionToken, FileAccess.ReadWrite, FileOptions.SequentialScan, 0);

以及这里的参数是他们得到的值:

filePath = "\\\\LAPTOP-PC\\VIDEOPRESENTERDB\\v02-A60EC2F8-2B24-11DF-9CC3-AF2E56D89593\\Archive\\dbo\\Records\\Picture\\C94D4189-9ECF-448B-B05A-ABF9331BF6CE\\VolumeHint-HarddiskVolume2"

obj有16个数字,范围从0到255.

我显然在某个地方犯了一个错误,但我不知道究竟在哪里

0 个答案:

没有答案