简体   繁体   English

显示数据库中的图像

[英]Display an image from a database

I have a simple database where i have book information stored in it. 我有一个简单的数据库,其中存储了图书信息。 2 Fiction books and 2 category books.. 2本小说书和2本分类书..

For example: a technical book; 例如:技术书籍; name, description, isbn, author,publication,price,image 名称,说明,isbn,作者,出版物,价格,图像

In the database i am not storing the actually image the image row just has the name of the image. 在数据库中,我没有存储实际的图像,图像行仅具有图像名称。 for example cprog.jpeg i think in index 8 例如cprog.jpeg我认为在索引8中

I have the image stored in the images file in the project. 我将图像存储在项目的图像文件中。 Now, i am displaying the books in a div rather than a gridview. 现在,我在div中而不是在gridview中显示书籍。 which is displaying fine, except i dont know how to do the image. 它显示的很好,除非我不知道该怎么做。

My question is how can i display the correct image from the database? 我的问题是如何显示数据库中的正确图像? Keep in mind i have several images. 请记住,我有几个图像。

strBooksInCategory = "<div>";

            foreach (DataTable table in dsgrid2.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    strBooksInCategory +=
                          "<div style=\"height:150px;\">"  
                        + "  <div style=\"border-style:solid;height:100px;width:100px;float:left;\"> Image </div>"
                        + "  <div style=\"height:110px;float:left;padding-left:10px;\">"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[0] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[1] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[2] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[3] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[4] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[5] + "</div>"
                        + " </div>"
                        + "</div>";
                    strBooksInCategory += "<div style=\"height:10px;width=100%;\"></div>";
                    //foreach (DataColumn column in table.Columns)
                    //{
                    //    strBooksInCategory += "<div style=\"border-style:solid;\">" + row[column] + "</div>";
                    //}
                }
            }
            strBooksInCategory += "</div>";

Here is my function on how i am retiriving the data from the database: 这是我如何从数据库检索数据的函数:

private DataSet GetDataSet2()
    {
        // SQLiteConnection sqlite = new SQLiteConnection("Data Source=/path/to/file.db");
        //SQLiteConnection sqlite = new SQLiteConnection( "Data Source=" + Server.MapPath(@"~\App_Data\bookDB.db"));
        // SQLiteDataAdapter ad;

        string cid = Server.UrlDecode(Request.QueryString["Category"]);
        //string Cname = Request.QueryString["CategoryDescription"];
        lblCategory.Text = cid;
        DataSet ds2 = new DataSet();
        // this.TextBox1.Text = cid;

        String connectionString = "Data Source=" + Server.MapPath(@"~\App_Data\bookDB.db");
        String selectCommand = String.Format("Select * from Book where CategoryName = '{0}'", Request.QueryString);
        SQLiteConnection myConnection = new SQLiteConnection();
        myConnection.ConnectionString = connectionString;
        myConnection.Open();
        SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(selectCommand, myConnection);

        myConnection.Close();
        // DataSet ds = new DataSet();
        dataAdapter.Fill(ds2);
        return ds2;
    }

Here is what the page looks like so far using the above code to display my 2 books in div 这是到目前为止使用以上代码在div中显示我的2本书的页面外观

在此处输入图片说明

如果这只是HTML,请使用<img src="url" alt="some_text"/> ,其中url将是存储图像的目录和图像名称的组合。

I think what they are saying isdont store the images in the db. 我认为他们说的是不要将图像存储在数据库中。 Store theimages on disk ina folder. 将图像存储在文件夹中的磁盘上。 Store just the image name in the db and then just append the image name to the path where the images are located. 将图像名称仅存储在数据库中,然后将图像名称附加到图像所在的路径即可。

Where you have 你在哪里

 + "  <div style=\"border-style:solid;height:100px;width:100px;float:left;\"> Image </div>" 

You need the tag but you can still use a variable to show the correct image. 您需要标记,但仍可以使用变量来显示正确的图像。 Save the root path of the image directory in one variable. 将映像目录的根路径保存在一个变量中。 Then append the unique image name. 然后附加唯一的图像名称。

imagepath = imagedir + row[n];

Then use that in your repeating rows: 然后在重复行中使用它:

 + "<img src='" + imagepath + "' alt='' height='100' width='100'>"

You may need to work your css a bit to get it all positioned right, but this will get your dynamic images into each row. 您可能需要花一点点css来使其正确放置,但这将使动态图像进入每一行。

use html image tag, , mention src property with filename which is returned by dataset. 使用html图像标记,并提及src属性以及文件名,该文件名由数据集返回。 and one more thing don use Select * from Use only column name which are required, which makes more secured. 还有一件事情不要使用Select * fromSelect * from仅使用必需的列名中Select * from ,这可以提高安全性。

Suggestion for this 对此的建议

  1. Store all FileName with GUID, so that they will be unique 使用GUID存储所有FileName,以便它们是唯一的
  2. Use "<img src='" + Your Dataset ReturnedFileName + "'/>" 使用"<img src='" + Your Dataset ReturnedFileName + "'/>"
  3. Also Use repeaters, which suits your scenario, instead of looping and creating, 另外,请使用适合您情况的中继器,而不是循环创建,

Repeater example 中继器示例

Repeater Documentation 中继器文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM