2009-07-27 6 views

Répondre

11

Il suffit de spécifier binaire comme type de paramètre, sa valeur peut être un byte[]

byte[] data; // wherever this comes from 

using (SqlCommand command = new SqlCommand()) 
{ 
    command.Connection = connection; 
    command.CommandText = "INSERT INTO BinaryTable (BinaryData) VALUES (@BinaryData)"; 

    SqlParameter param = new SqlParameter("@BinaryData", SqlDbType.Binary); 
    param.Value = data; 

    command.Parameters.Add(param); 
    command.ExecuteNonQuery(); 
} 

Edit: Il faut également noter que si vous utilisez SQL Server 2005/2008, vous devez utiliser VARBINARY(MAX) à la place de IMAGE puisque ce dernier est déprécié.

0

Voici une fonction exemple

public bool AddCompanyIcon(string MakeName, byte[] BytesOriginal,string ImageName) 
    { 
     try 
     { 
      System.Data.SqlClient.SqlParameter[] ImgPara = new SqlParameter[3]; 
      ImgPara[0] = new SqlParameter("@MakeName", MakeName); 
      ImgPara[1] = new SqlParameter("@MakeIcon", BytesOriginal); 
      ImgPara[2] = new SqlParameter("@ImageName", ImageName); 

      db.ExecuteScalerSP("sp_AddAutoCompanyLogo", ImgPara); 
      db.CloseConnection(); 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 

Ci-dessous est sp_AddAutoCompanyLogo procédure stockée

ALTER PROCEDURE [dbo].[sp_AddAutoCompanyLogo] 
    -- Add the parameters for the stored procedure here 
    @MakeName varchar(50), 
    @MakeIcon image, 
    @ImageName varchar(50) 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 
    -- Insert statements for procedure here 
    insert into IVAutoGalleryLogos(MakeName,MakeIcon,ImageName) 
    values(upper(@MakeName),@MakeIcon,@ImageName) 
END 

Espérons que cela aidera ...

Questions connexes