當前位置:首頁 » 文件管理 » c圖片上傳資料庫

c圖片上傳資料庫

發布時間: 2023-02-05 03:36:06

① c#如何把圖片存取到sql資料庫

一樓開玩笑了!!

可以保存到資料庫的。
首先,你的資料庫里要有一個存放二進制數據的欄位。
然後,用一個文件選擇控制項,讓用戶選擇圖片。
用FileStream.Read把圖片文件按照二進制讀取到byte[]中,
接下來,鏈接資料庫,用sql語句,進行相應的插入操作,將資料庫的二進制數據的欄位賦值為byte[]就行了。

以下是保存和顯示的代碼:
private void SaveImage(string fileName)
{
// Read the file into a byte array
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
byte[] imageData = new Byte[fs.Length];
fs.Read(imageData, 0, (int)fs.Length);

using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = "insert into image (imagefilename,blobdata) values (@filename,@blobdata)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@filename",SqlDbType.Text);
cmd.Parameters["@filename"].Direction = ParameterDirection.Input;
cmd.Parameters.Add("@blobdata", SqlDbType.Image);
cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;
// Store the byte array within the image field
cmd.Parameters["@filename"].Value = fileName;
cmd.Parameters["@blobdata"].Value = imageData;
conn.Open();
if (cmd.ExecuteNonQuery() == 1)
{
MessageBox.Show("Done");
}
}
}
}

private void LoadImage(string fileName)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = "select blobdata from Image where ImageFileName like @filename";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@filename", SqlDbType.Text);
cmd.Parameters["@filename"].Value = fileName;

conn.Open();

object objImage = cmd.ExecuteScalar();

byte[] buffer = (byte[])objImage;

BinaryWriter bw = new BinaryWriter(new FileStream("C:\\abcd.png", FileMode.Create));
bw.Write(buffer);
bw.Close();

MemoryStream ms = new MemoryStream(buffer);
Image bgImage = Image.FromStream(ms);
ms.Close();
this.BackgroundImage = bgImage;
}

}

------------------------------------------------------------------
PS:用空情報我踩踩空間,謝謝。
http://hi..com/kxl361

② VS.C#如何向數據資料庫中存入和讀取圖片的

首先,在資料庫中要建立相應的欄位能保存Bytes,例如在SQL Server中用Image類型來定義欄位。我所用到的資料庫大致結構如下:

欄位名
類型
備注

FileID
Int
自增欄位

FileName
Varchar(256)

FullName
Varchar(1024)

FileData
Image

然後就是寫入資料庫,代碼如下:

FileInfo fi = new FileInfo( txtFileName.Text );// Replace with your file name

if ( fi.Exists)

{

byte[] bData = null;

int nNewFileID = 0;

// Read file data into buffer

using ( FileStream fs = fi.OpenRead() )

{

bData = new byte[fi.Length];

int nReadLength = fs.Read( bData,0, (int)(fi.Length) );

}

// Add file info into DB

string strQuery = "INSERT INTO FileInfo "

+ " ( FileName, FullName, FileData ) "

+ " VALUES "

+ " ( @FileName, @FullName, @FileData ) "

+ " SELECT @@IDENTITY AS 'Identity'";

SqlCommand sqlComm = new SqlCommand( strQuery, sqlConn );

sqlComm.Parameters.Add( "@FileName", fi.Name );

sqlComm.Parameters.Add( "@FullName", fi.FullName );

sqlComm.Parameters.Add( "@FileData", bData );

// Get new file ID

SqlDataReader sqlReader = sqlComm.ExecuteReader();

if( sqlReader.Read() )

{

nNewFileID = int.Parse(sqlReader.GetValue(0).ToString());

}

sqlReader.Close();

sqlComm.Dispose();

if( nNewFileID > 0 )

{

// Add new item in list view

ListViewItem itmNew = lsvFileInfo.Items.Add( fi.Name );

itmNew.Tag = nNewFileID;

}

}

而讀出的代碼如下:

// Get new file name

string strFullName = dlgFBSave.SelectedPath;

if( strFullName[strFullName.Length - 1] != '\\' )

strFullName += @"\";

strFullName += lsvFileInfo.SelectedItems[0].Text;

string strQuery = "SELECT FileData FROM FileInfo "

+ " WHERE FileID = " + lsvFileInfo.SelectedItems[0].Tag.ToString();

SqlDataAdapter sqlDAdapter = new SqlDataAdapter(strQuery,sqlConn);

DataSet sqlRecordSet = new DataSet();

byte[] bData = null;

//Get file data from DB

try

{

sqlDAdapter.Fill( sqlRecordSet, "FileInfo" );

foreach( DataRow dr in sqlRecordSet.Tables["FileInfo"].Rows)

{

if( dr["FileData"] != DBNull.Value )

bData = ( byte[] )dr["FileData"];

}

}

catch(SqlException sqlErr)

{

MessageBox.Show( sqlErr.Message );

}

catch

{

MessageBox.Show( "Failed to read data from DB!" );

}

sqlRecordSet.Dispose();

sqlDAdapter.Dispose();

if( bData != null )

{

// Save file

FileInfo fi = new FileInfo( strFullName );

if( !fi.Exists )

{

//Create the file.

using (FileStream fs = fi.Create())

{

fs.Write( bData, 0, bData.Length);

}

}

else

{

//Create the file.

using (FileStream fs = fi.OpenWrite())

{

fs.Write( bData, 0, bData.Length);

}

}

}

不過需要提的一點,如果把大量的文件存入資料庫的話,會造成資料庫的臃腫,而且訪問量也會增大。所以現在比較流行的做法,是把文件上傳到伺服器上,而在資料庫上只保存文件的相對路徑即可。那麼訪問的時候,先通過資料庫得到文件的相對路徑,然後再訪問伺服器上的文件

③ 如何將上傳圖片和文字寫進資料庫

圖片放在本地的某個目錄中如「c:\image」,然後再把這個目錄+圖片名存入資料庫"c:\image\image1.jpg"。讀出時只要使用<img
href="c:\image\image1.jpg">就OK了。除此之外還可以把上傳的圖片作為位元組流讀入資料庫,讀出時使用cgi或Servlet程序把讀出的流以圖片顯示出來。

④ C#winform 中上傳圖片保存到資料庫中

就是2中方法:
1:上傳圖片的相對路徑到資料庫中相應欄位里,讀取顯示時,將控制項(假設用的是Image控制項)的ImageUrl屬性指向該相對路徑即可。

2:將圖片以二進制流的方式整體上傳到資料庫里,讀取顯示時,以二進制流的方式整體讀出。這種方法稍微麻煩一點,但保存的是圖片整體到資料庫里。

⑤ vc mfc中怎麼通過按鈕 「上傳圖片」把本機上的圖片讀入SQL資料庫

圖片就是文件嘛,把文件數據全部讀入到內存然後插入到sql資料庫中就可以了,但不建議這樣做,因為圖片數據比較大,存入資料庫會很慢,檢索也會很慢,建議只存入圖片的路徑,比如你要存入的圖片為c:\\test.bmp,那麼你存入數據的信息就為c:\\test.bmp,當有地方要使用該圖片時,只需取出c:\\test.bmp這個路徑就可以操作該圖片了。

⑥ C#控制台程序怎麼將本地圖片批量插入sql server 2008資料庫,請看補充,請大神幫忙給出代碼,有追加!

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
usingSystem.Data.SqlClient;
usingSystem.Data;

namespaceImportImage
{
classProgram
{
staticvoidMain(string[]args)
{
stringconn="server=.;database=testDB;Uid=sa;Pwd=sa";
using(SqlConnectionmyconn=newSqlConnection(conn))
{
myconn.Open();
using(SqlCommandmycomm=newSqlCommand())
{
DirectoryInfodir=newDirectoryInfo(@"C:rimages");
foreach(FileInfoitemindir.GetFiles("*.jpg"))
{
stringstr=string.Format("insertinto[資料庫中的表的名字](imagAns)values(@file)",item.Name);//假設你的id是自動增長的
mycomm.CommandText=str;
mycomm.Connection=myconn;
FileStreamfs=newFileStream(item.FullName,FileMode.Open);
BinaryReaderbr=newBinaryReader(fs);
Byte[]byData=br.ReadBytes((int)fs.Length);
fs.Close();
mycomm.Parameters.Add("@file",SqlDbType.Binary,byData.Length);
mycomm.Parameters["@file"].Value=byData;
mycomm.ExecuteNonQuery();
}
}
}
}
}
}

⑦ 有一個圖片保存在"c:/123/123.jpg",如何把它的路徑保存到資料庫並讀出來

在wwwroot目錄建立一個目錄,例如images,將圖片保存在這個目錄中,在資料庫建立一個欄位,例如LJ,在這個欄位中輸入:/images/123.jpg即可。

⑧ vc mfc中怎麼通過按鈕 「上傳圖片」把本機上的圖片讀入SQL資料庫

圖片就是文件嘛,把文件數據全部讀入到內存然後插入到
sql資料庫
中就可以了,但不建議這樣做,因為圖片數據比較大,存入資料庫會很慢,檢索也會很慢,建議只存入圖片的路徑,比如你要存入的圖片為c:\\test.bmp,那麼你存入數據的信息就為c:\\test.bmp,當有地方要使用該圖片時,只需取出c:\\test.bmp這個路徑就可以操作該圖片了。

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:712
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:975
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:686
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:837
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:744
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1085
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:314
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:194
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:882
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:840