温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - SQL output parameter of varbinary to use as image on ASPX web form

c# - 在ASPX Web表单上用作图像的varbinary的SQL输出参数

发布于 2020-03-29 21:42:38

我正在尝试检索存储在表中的徽标,并将图像从数据库显示到我的aspx网页。我在SQL存储过程中有输出参数,该输出参数输出名称和varbinary数据类型。在SQL表中,第一列是名称,它是NVARCHAR(100),第二列是图像徽标。使用SQL SERVER for DBMS。

例如,客户端1是名称和标志看起来像这样:0x89504E470D0A1A0A0000000D49484452000000C8000000C80802000000223A39C9000000097048597300002E2300002E230178A53F76000000206348524D00007A25000080830000F9FF000080E9000075300000EA6000003A980000176F925FC546000012954944415478DAEC9D79741455BE80BFEA4EBA3B0B09092124806149D80504C20E0282FA044591F500E2809C0047715F8E8EC3E0E8F846F1313A7014C4055704065 ...

插入表中的示例SQL代码:

INSERT INTO dbo.client
SELECT 'Client1', BulkColumn 
FROM Openrowset( Bulk 'F:\logo.png', Single_Blob) as img

用C#代码调用的存储过程:

 CREATE PROCEDURE [dbo].[sp_ui_getClientNameLogo]
    -- Add the parameters for the stored procedure here
    @ClientName NVARCHAR(100) OUTPUT,
    @Logo varbinary(max) OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SET @ClientName = (SELECT Client_Name FROM dbo.client)

    SET @Logo = (SELECT Logo FROM dbo.client)

END

C#-Index.aspx.cs:

        public static void getClientInfo()
    {
        string connectionstring = ConfigurationManager.ConnectionStrings["SQLConString"].ConnectionString;
        SqlConnection cn = new SqlConnection(connectionstring);
        cn.Open();
        SqlCommand cmd = new SqlCommand("dbo.sp_ui_getClientNameLogo", cn);
        cmd.Parameters.Add(new SqlParameter("@ClientName", SqlDbType.NVarChar, 100, ParameterDirection.Output, false,0, 10, "ClientName", DataRowVersion.Default, null));
        cmd.Parameters.Add(new SqlParameter("@Logo", SqlDbType.VarBinary, 1000, ParameterDirection.Output, false, 0, 10, "Logo", DataRowVersion.Default, null));
        var da = new SqlDataAdapter(cmd);
        cmd.CommandType = CommandType.StoredProcedure;
        var dt = new DataTable();
        da.Fill(dt);

        cn.Close();

        clientName = cmd.Parameters["@ClientName"].Value.ToString();
        **logoBinary = cmd.Parameters["@Logo"].Value. //THIS IS WHERE I AM STUCK**

    }

如何获取徽标的输出变量并将其存储为图像以在网页上使用?

查看更多

提问者
roa765
被浏览
22
Atk 2020-01-31 19:43

试试这个代码

在aspx页面上

     <img id='yourID' runat='server'/>

在CS页面上

      File.WriteAllBytes(yourfilename, (byte[])(cmd.Parameters["@Logo"].Value));
      byte[] logoBinary = (byte[])(cmd.Parameters["@Logo"].Value);
      string base64string= Convert.ToBase64String(logoBinary, 0, logoBinary.Length);
      yourID.Src = "data:image/jpg;base64," + base64String;

发布
问题

分享
好友

手机
浏览

扫码手机浏览