如何将varbinary对象从数据库显示/绑定到gridview?

问题描述 投票:0回答:2

我正在尝试将数据库中的数据绑定到gridview中。我的一个列包含varbinary(MAX)数据类型。当我绑定它时,我的gridview显示System.Byte []而不是实际数据。

我试图反序列化对象,但我不知道从那里去哪里?我也尝试过使用自己的ItemTemplate。

带VARBINARY的表:

CREATE TABLE [dbo].[TP_ShoppingCart] (
    [ShoppingCartID] INT             IDENTITY (1, 1) NOT NULL,
    [Email]          VARCHAR (250)   NOT NULL,
    [ShoppingCart]   VARBINARY (MAX) NULL,
    PRIMARY KEY CLUSTERED ([ShoppingCartID] ASC),
    CONSTRAINT [FK_TP_ShoppingCart_TP_CustomerAccts] FOREIGN KEY ([Email]) REFERENCES [dbo].[TP_CustomerAccounts] ([Email])
);

存储过程:

CREATE PROCEDURE [dbo].TP_GetSalesByCustomer
    @theEmail varchar(250)
AS
    SELECT * FROM TP_Sales s JOIN TP_ShoppingCart sa ON s.ShoppingCartID = sa.ShoppingCartID
    WHERE s.Email = @theEmail AND sa.Email = @theEmail;

Gridview代码:

<asp:Panel ID="SalesPanel" runat="server" CssClass="col-sm-8 col-md-8 col-lg-8 table-bordered">
            <h1><small><span class="glyphicon glyphicon-th-list" aria-hidden="true"></span></small> Customer Sales</h1>
            <br />
            <br />
            <asp:GridView ID="gvSales" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered table-hover">
                <Columns>
                    <asp:BoundField DataField="SalesID" HeaderText="Sales ID" SortExpression="SalesID" />
                    <asp:BoundField DataField="SaleDate" HeaderText="Date of Sale" DataFormatString="{0:d}" />
                    <asp:TemplateField HeaderText="Products">
                        <ItemTemplate>
                            <asp:Label ID="lblShoppingCart" runat="server" Text='<%# Eval("ShoppingCart") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="ShoppingCart" HeaderText="Products" DataFormatString="" />
                    <asp:BoundField DataField="SaleTotal" HeaderText="Sale Total" DataFormatString="{0:C0}" />
                </Columns>
            </asp:GridView>

</asp:Panel>

代码隐藏:

protected void btnSalesAccount_Click(object sender, EventArgs e)
        {
            CustomerAccountPanel.Visible = false;
            UpdateAccountPanel.Visible = false;
            CustomerCCPanel.Visible = false;
            UpdatePasswordPanel.Visible = false;
            SalesPanel.Visible = true;
            gvSales.Visible = true;

            DBConnect objDB = new DBConnect();
            SqlCommand objCmd = new SqlCommand();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "TP_GetSalesByCustomer";

            //string email = Session["custEmail"].ToString();

            objCmd.Parameters.AddWithValue("@theEmail", email);

            gvSales.DataSource = objDB.GetDataSetUsingCmdObj(objCmd);
            gvSales.DataBind();

c# asp.net gridview aspxgridview varbinary
2个回答
1
投票

试试这个:

SELECT CONVERT(varchar(MAX),yourvarbincolumn,0)

Here是关于风格选择的更多细节(第三个参数)。

并且,在这里您需要将二进制数据类型转换为varchar数据类型,让我们从您的示例中选择它:

CONVERT(VARCHAR(MAX),[dbo]。[TP_ShoppingCart]。[ShoppingCart],1)AS ShoppingCart


0
投票
SELECT CONVERT(VARCHAR(MAX), your_varbinary_column, 1) AS for_display
FROM your_table

将varbinary(max)列转换为varchar,就像我提到的代码一样。

要么

访问:http://geekswithblogs.net/dotNETvinz/archive/2009/04/24/faq-displaying-image-from-database-to-gridview-control.aspx

对于你可以使用的替代方法。

© www.soinside.com 2019 - 2024. All rights reserved.