【问题标题】:How to create an image column in a SQL Server table and retrieve it in ASP.NET using VB如何在 SQL Server 表中创建图像列并使用 VB 在 ASP.NET 中检索它
【发布时间】:2012-03-22 01:47:44
【问题描述】:

我想知道如何在 SQL Server 表中创建图像列并使用 VB.NET 在 ASP.NET 中检索它

create table Tbl(ID int primary key,Name varchar(20),Image ....)

【问题讨论】:

    标签: asp.net sql sql-server vb.net sql-server-2008


    【解决方案1】:

    您想使用VARBINARY(MAX) 来存储此数据。

    代码方面,here is a good, brief answer.

    来自答案:

    FileStream st = new FileStream(@"C:\filename.jpg", FileMode.Open);
    byte[] buffer = new byte[st.Length];
    st.Read(buffer, 0, (int)st.Length);
    st.Close();
    
    SqlConnection conn = new SqlConnection("...");
    SqlCommand cmd = new SqlCommand(
        "UPDATE SomeTable SET image=@image WHERE ID = 1", conn);
    cmd.Parameters.AddWithValue("@image", buffer);
    conn.Open();
    int i = cmd.ExecuteNonQuery();
    conn.Close();
    

    【讨论】:

    • pllz Justin 如何不使用任何 VB 方法直接在 sql 列中添加图像?
    • 我想在表格的一列中插入图像已经有 5 列我尝试这个但它不起作用插入到 dbo.Produit 值('Pc 便携式','HP EliteBook série p ','Un ordinateur professionalnel robuste de 35,5 et 39,6 cm (14.0" et 15.6") à fonctions multiples, hautes performances et longue autonomie',SELECT * FROM OPENROWSET(BULK N'C:\Users\Yassine-Kira \Desktop\Templates\ProductImg\elite-book_tcm_133_1096796.png', SINGLE_BLOB) ,20,4999,0);
    • 这是你要找的东西吗:stackoverflow.com/questions/416881/…
    【解决方案2】:

    【讨论】:

    • pllz JotaBa 如何在不使用任何 VB 方法的情况下直接在 sql 中添加图像?
    【解决方案3】:

    在 sql server 中创建类型为:image 的列。 在 asp 中,使用这个示例代码:(它在 c# 中)

        string con = "your connection string"
        var Image =  (from A in YourTblImage
                      select A).First();//to get one record of the table use the `first`
        FileStream fs = new FileStream(@"yourPath to save the image", FileMode.Create);
        try
        {
    
            System.Data.Linq.Binary img = image.imageColumnName;
            byte[] imageK = img.ToArray();
            fs.Write(imageK, 0, imageK.Length);
        }
        catch (Exception ex)
        {
            string str = ex.Message;
        }
        finally
        {
            fs.Close();
        }
    

    【讨论】:

    • tnkks 我的兄弟 ^^ 我想将图像插入到表格的列中,已经有 5 列我尝试这样做,但它不起作用插入 dbo.Produit 值('PC 便携', 'HP EliteBook série p','Un ordinateur professionalnel robuste de 35,5 et 39,6 cm (14.0" et 15.6") à fonctions multiples, hautes performances et longue autonomie',SELECT * FROM OPENROWSET(BULK N'C:\ Users\Yassine-Kira\Desktop\Templates\ProductImg\elite-book_tcm_133_1096796.png', SINGLE_BLOB) ,20,4999,0);
    猜你喜欢
    • 2021-09-05
    • 2010-10-26
    • 2020-10-09
    • 2014-03-19
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 2014-08-13
    • 2016-12-06
    相关资源
    最近更新 更多