0

私は C# アプリケーションを使用しており、画像をデータベースに挿入する準備ができているように見えますが、ストアド プロシージャは暗黙的な変換エラーを吐き出します。イメージをバイト配列に読み取り、そのバイト配列をストアド プロシージャに渡します。パラメータvarbinaryが必要なため、エラーが発生します。そこで、ストアド プロシージャを次のように変更します。

ALTER PROCEDURE insertPlayerImage 
      @playerID varchar(9), 
      @profileImage varchar(max), 
      @pending char(1)
AS
    CONVERT(varbinary(max), @profileImage)

INSERT INTO PlayerImage(playerID, profileImage, pending)
VALUES(@playerID, @profileImage, @pending)
GO

これは、varchar(私のバイト配列)が必要であり、配列をvarbinaryファイルに変換することを示しています。私のストアドプロシージャは、私が持っている変換行が好きではありません。しかし、私が単にそうするなら

SELECT CONVERT(varchar, GETDATE());  

できます。すべての Google 検索は、日付の変換を指しています。まるで変換を使用できる唯一のものであるかのようです。

4

3 に答える 3

0

SQL Server を使用していますか? その場合は、SQL データ型から CLR データ型へのマッピングについて、次のページを参照してください: http://msdn.microsoft.com/en-us/library/cc716729.aspx

SQL Server charvarcharncharおよびnvarcharすべてが C# との間でマップされますstring(ただし、char[]も同様に機能します)。

SQL Server binary andvarbinary map to/from a C#byte[]`.

あなたが抱えている実際の問題は何ですか?

さらに、バイナリ データを varchar として SQL Server に渡している場合、UTF-16 (CLR 内部文字列エンコーディング) と SQL Server が使用しているコード ページとの間の変換で変更されることが予想されます。

注意すべきもう1つのこと:ストアドプロシージャ:

ALTER PROCEDURE insertPlayerImage 
  @playerID varchar(9), 
  @profileImage varchar(max), 
  @pending char(1)
AS

  CONVERT(varbinary(max), @profileImage)

  INSERT INTO PlayerImage
  ( playerID , profileImage , pending )
  VALUES
  ( @playerID , @profileImage , @pending )

GO

は正当な SQL ではありません。Convert()は関数であり、SQL ステートメントではありません。コンパイルさえしません。varcharパラメータ@profileImageをに変換しようとしている場合はvarbinary、次の行に沿って何かを行う必要があります

 declare @image varbinary(max)
 set @image = convert(varbinary(max),@profileImage)

ストアド プロシージャに署名がある場合

create procedure dbo.insertPlayerImage

  @playerId     varchar(9) ,
  @profileImage varbinary(max) ,
  @pending      char(1)

as
...

次に、このコードは次のことを行います。

public int insertProfileImage( string playerId , byte[] profileImage , bool pending )
{
  if ( string.IsNullOrWhiteSpace(playerId) ) throw new ArgumentException("playerId" ) ;
  if ( profileImage == null || profileImage.Length < 1 ) throw new ArgumentException("profileImage") ;

  int rowCount ;

  string connectString = GetConnectString() ;
  using ( SqlConnection connection = new SqlConnection(connectString) )
  using ( SqlCommand command = connection.CreateCommand() )
  {

    command.CommandType = CommandType.StoredProcedure ;
    command.CommandText = "dbo.insertPlayerImage" ;

    command.Parameters.AddWithValue( "@playerId"     , playerId            ) ;
    command.Parameters.AddWithValue( "@profileImage" , profileImage        ) ;
    command.Parameters.AddWithValue( "@pending"      , pending ? "Y" : "N" ) ;

    rowCount = command.ExecuteNonQuery() ;

  }

  return rowCount ;
}

ただし、null画像データに を渡す場合は、パラメーターの値を設定する方法を変更する必要があります。次のようなもの:

command.Parameters.AddWithValue( "@profileImage" , profileImage != null ? (object)profileImage : (object)DBNull.Value ) ;

または

SqlParameter p = new SqlParameter( "@profileImage" , SqlDbType.VarBinary ) ;
p.Value = DBNull.Value ;
if ( profileImage != null )
{
  p.Value = profileImage ;
}
command.Parameters.Add( p ) ;
于 2013-05-15T18:06:00.853 に答える