當前位置:首頁 » 健康資訊 » 資料庫可以存圖片嗎
擴展閱讀
怎樣讓別人添加騰訊會員 2025-05-22 03:32:13

資料庫可以存圖片嗎

發布時間: 2022-03-09 13:41:33

『壹』 圖片如何存入資料庫

1、新建一個資料庫,資料庫名為Image,表名為image。並為表添加ID,tupian兩個列。

『貳』 如何像資料庫中保存圖片

一般圖像是不保存在資料庫的.而是先將圖片放在工程下的某個文件夾中,將圖片所在的工程文件路徑存在資料庫中,當程序載入圖片的時候,從資料庫中讀取圖片的路徑,然後根據路徑在工程的文件夾中讀取圖片文件

『叄』 SQL資料庫中能存照片嗎

數據中可以存儲圖片,但是需要注意不能直接存儲圖片,而是轉換成二進制或者Base64等的「文本」來存儲,在用的時候,可以再轉換回來。

在網站開發中,一般將圖片存儲在文件系統中,而不是數據系統中,資料庫系統中只記錄圖片在文件系統中的路徑而已。

拓展資料:

SQL是Structured Query Language(結構化查詢語言)的縮寫。SQL是專為資料庫而建立的操作命令集,是一種功能齊全的資料庫語言。在使用它時,只需要發出「做什麼」的命令,「怎麼做」是不用使用者考慮的。SQL功能強大、簡單易學、使用方便,已經成為了資料庫操作的基礎,並且現在幾乎所有的資料庫均支持SQL。

網路_SQL資料庫

『肆』 資料庫可以存圖片嗎

都可以的,至於怎麼實現,建議你多去www.csdn.net看看吧,裡面有很多高手,他們會教你的。

access是可以存儲圖片的,不是存儲網址,至於如何用C#向ACCESS資料庫插入圖片?給你個例子,你慢慢研究一下吧,不過這樣例子的版本是VS2003,不過是差不多的。
-------------------------------------
<%@ Page language="c#" Debug="true" Codebehind="Image2Access.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.Image2Access" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>上傳文件到 Access 資料庫</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="DataGridShowImage" method="post" runat="server" enctype="multipart/form-data">
<h3 align="center">上傳文件到 Access 資料庫</h3>
<asp:DataGrid ID="DG_Persons" AutoGenerateColumns="False" Width="99%" HeaderStyle-BackColor="#ff0000"
HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#ffffff" ItemStyle-BackColor="Beige" BorderColor="#000000"
Runat="server" HeaderStyle-HorizontalAlign="Center">
<Columns>
<asp:TemplateColumn HeaderText="姓名">
<ItemTemplate>
<asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonName") %>' ID="Label1"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="電子郵件">
<ItemTemplate>
<asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonEmail") %>' ID="Label2"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="性別">
<ItemTemplate>
<asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonSex") %>' ID="Label3"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="照片">
<ItemTemplate>
<asp:Image Runat=server ID="Image1" ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "PersonID")) %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<b>文件名字:</b><input id="MyFileName" type="text" runat="server" NAME="MyFileName">
<P>
<b>文件:</b><input id="MyFile" type="file" runat="server" NAME="MyFile">
<br>
<br>
<input type="submit" value="開始上傳" runat="server" ID="Submit1" NAME="Submit1">
</P>
</form>
</body>
</HTML>
----------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace eMeng.Exam
{
/// <summary>
/// Image2Access 的摘要說明。
/// </summary>
public class Image2Access : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputText MyFileName;
protected System.Web.UI.HtmlControls.HtmlInputFile MyFile;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
protected System.Web.UI.WebControls.DataGrid DG_Persons;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
BindGrid();
}
private void BindGrid()
{
string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +Server.MapPath("Image2Access.mdb");
OleDbConnection myConnection = new OleDbConnection(strCnn);
OleDbCommand myCommand = new OleDbCommand("SELECT * FROM Person", myConnection);
myCommand.CommandType = CommandType.Text;
try
{
myConnection.Open();
DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
DG_Persons.DataBind();
}
catch(OleDbException SQLexc)
{
Response.Write("提取數據時出現錯誤:" + SQLexc.ToString());
}
}
protected string FormatURL(object strArgument)
{
return "ReadImage.aspx?id=" + strArgument.ToString();
}

#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Submit1.ServerClick += new System.EventHandler(this.Submit1_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Submit1_ServerClick(object sender, System.EventArgs e)
{
//得到提交的文件
Stream fileDataStream = MyFile.PostedFile.InputStream;

//得到文件大小
int fileLength = MyFile.PostedFile.ContentLength;

//創建數組
byte[] fileData = new byte[fileLength];

//把文件流填充到數組
fileDataStream.Read(fileData,0,fileLength);

//得到文件名字
string fileTitle = MyFileName.Value;

//得到文件類型
string fileType = MyFile.PostedFile.ContentType;

//構建資料庫連接,SQL語句,創建參數
string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Image2Access.mdb");
OleDbConnection myConnection = new OleDbConnection(strCnn);
OleDbCommand command = new OleDbCommand ("INSERT INTO Person (PersonName,PersonEmail,PersonSex,PersonImageType,PersonImage)" +
"VALUES (@PersonName,@PersonEmail,@PersonSex,@PersonImageType,@PersonImage)", myConnection);

System.Data.OleDb.OleDbParameter paramPersonName = new OleDbParameter("@PersonName", System.Data.OleDb.OleDbType.VarChar,50);
paramPersonName.Value = fileTitle;
command.Parameters.Add(paramPersonName);

System.Data.OleDb.OleDbParameter paramPersonEmail = new OleDbParameter("@PersonEmail", System.Data.OleDb.OleDbType.VarChar,50);
paramPersonEmail.Value = "[email protected]";
command.Parameters.Add(paramPersonEmail);

System.Data.OleDb.OleDbParameter paramPersonSex = new OleDbParameter("@paramPersonSex", System.Data.OleDb.OleDbType.VarChar,50);
paramPersonSex.Value = "男";
command.Parameters.Add(paramPersonSex);

System.Data.OleDb.OleDbParameter paramPersonImageType = new OleDbParameter("@PersonImageType", System.Data.OleDb.OleDbType.VarChar,50);
paramPersonImageType.Value = fileType;
command.Parameters.Add(paramPersonImageType);

System.Data.OleDb.OleDbParameter paramPersonImage = new OleDbParameter("@PersonImage", System.Data.OleDb.OleDbType.Binary);
paramPersonImage.Value = fileData;
command.Parameters.Add(paramPersonImage);

//打開連接,執行查詢
myConnection.Open();
command.ExecuteNonQuery();
myConnection.Close();

}
}
}

『伍』 資料庫中可以存儲照片么怎麼存儲

數據中可以存儲圖片,但是需要注意不能直接存儲圖片,而是轉換成二進制或者Base64等的「文本」來存儲,在用的時候,可以再轉換回來。
在網站開發中,一般將圖片存儲在文件系統中,而不是數據系統中,資料庫系統中只記錄圖片在文件系統中的路徑而已。

『陸』 圖片如何存入資料庫

通常對用戶上傳的圖片需要保存到資料庫中。解決方法一般有兩種:一種是將圖片保存的路徑存儲到資料庫;另一種是將圖片以二進制數據流的形式直接寫入資料庫欄位中。以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
string uppath="";//用於保存圖片上傳路徑
//獲取上傳圖片的文件名
string fileFullname = this.FileUpload1.FileName;
//獲取圖片上傳的時間,以時間作為圖片的名字可以防止圖片重名
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
//獲取圖片的文件名(不含擴展名)
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
//獲取圖片擴展名
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
//判斷是否為要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")
{
//將圖片上傳到指定路徑的文件夾
this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type);
//將路徑保存到變數,將該變數的值保存到資料庫相應欄位即可
uppath = "~/upload/" + dataName + "." + type;
}
二、將圖片以二進制數據流直接保存到資料庫:
引用如下命名空間:
using System.Drawing;
using System.IO;
using System.Data.SqlClient;
設計資料庫時,表中相應的欄位類型為iamge
保存:
//圖片路徑
string strPath = this.FileUpload1.PostedFile.FileName.ToString ();
//讀取圖片
FileStream fs = new System.IO.FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123");
string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操作資料庫語句根據需要修改
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
if (myComm.ExecuteNonQuery() > 0)
{
this.Label1.Text = "ok";
}
myConn.Close();
讀取:
...連接資料庫字元串省略
mycon.Open();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查詢語句根據需要修改
byte[] image = (byte[])command.ExecuteScalar ();
//指定從資料庫讀取出來的圖片的保存路徑及名字
string strPath = "~/Upload/zhangsan.JPG";
string strPhotoPath = Server.MapPath(strPath);
//按上面的路徑與名字保存圖片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//顯示圖片
this.Image1.ImageUrl = strPath;
採用倆種方式可以根據實際需求靈活選擇。

『柒』 圖片如何存儲在資料庫當中

可以倒是可以,可以把它們以二進制方式存入資料庫,但是基本上沒人那麼做,那樣資料庫太大了。
一般都是把圖片等多媒體文件直接儲存(上傳)後,然後把路徑存入資料庫中。

『捌』 資料庫怎麼存圖片數據

一般來說存相對路徑性能比較好,但需要你處理好數據和文件的同步問題
有的資料庫支持文件類型

『玖』 資料庫怎麼儲存圖片

資料庫存儲圖片,其實是存儲圖片在伺服器上的路徑或圖片的絕對地址 。它是一個字元串,所以資料庫欄位的類型可使用varchar【可變的,長度不超過255】。在前台調用時,需要將路徑放置在img標簽的src屬性中,即可顯示圖片