當前位置:首頁 » 操作系統 » 資料庫二進制文件保存

資料庫二進制文件保存

發布時間: 2022-06-29 13:59:16

1. 怎樣用VB將資料庫中的二進制文件保存到磁碟

我說下思路
varbinary 的資料庫你能取得到 ,通過sql語句或存儲過程 ,然後 new 一個數組,把這個數組寫入到文件里。文件打開的模式用 w |b 這樣的標識 。

2. 以二進制形式保存文件到資料庫 有什麼優點缺點呢請指教 資料庫是mysql,文件類型是doc和txt

優點,不用單獨管理文件了唄,文件數據都在資料庫里呢。用戶想訪問文件的話,你就可以做一些許可權檢查什麼的,通過才給它取數據。

缺點,資料庫稍微有些壓力唄~~~~資料庫文件會變大~~~

3. 怎麼在mysql中存儲二進制文件

php腳本為例。PHP一般只支持小於2M的文件,假如要存取大於2M的文件,那就要進系統方面的設置了。具體操作如下:


首先創建測試表testtable

CREATETABLEtesttable(idINT(5)NOTNULLAUTO_INCREMENTPRIMARYKEY,filenameCHAR(255),dataLONGBLOB);


將文件存入表中使用如下PHP代碼

<?php
mysql_connect("localhost","root","password");//連接資料庫
mysql_select_db("database");//選定資料庫
$filename=""//這里填入二進制文件名
$data=addslashes(fread(fopen($filename,"r"),filesize($filename)));//打開文件並規范化數據存入變數$data中
$result=mysql_query("INSERTINTOtesttable(filename,data)VALUES('$filename','$data')");//數據插入到資料庫test表中
mysql_close();
?>


從表中取迴文件,使用如下PHP代碼

<?php
if($id){
mysql_connect("localhost","root","password");
mysql_select_db("database");
$filename=""//這里填入二進制文件名
$query="=$filename";
$result=mysql_query($query);
$data=mysql_result($result,0,"data");
?>

4. 如何將txt,doc等文件以二進制形式保存在資料庫中

private int WriteToDB(string strName, string strType, ref byte[] Buffer)
{
int nFileID = 0;

// Create connection
OleDbConnection dbConn = new OleDbConnection(GetConnectionString());

// Create Adapter
OleDbDataAdapter dbAdapt = new OleDbDataAdapter("SELECT * FROM tblFile", dbConn);

// We need this to get an ID back from the database
dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey;

// Create and initialize CommandBuilder
OleDbCommandBuilder dbCB = new OleDbCommandBuilder(dbAdapt);

// Open Connection
dbConn.Open();

// New DataSet
DataSet dbSet = new DataSet();

// Populate DataSet with data
dbAdapt.Fill(dbSet, "tblFile");

// Get reference to our table
DataTable dbTable = dbSet.Tables["tblFile"];

// Create new row
DataRow dbRow = dbTable.NewRow();

// Store data in the row
dbRow["FileName"] = strName;
dbRow["FileSize"] = Buffer.Length;
dbRow["ContentType"] = strType;
dbRow["FileData"] = Buffer;

// Add row back to table
dbTable.Rows.Add(dbRow);

// Update data source
dbAdapt.Update(dbSet, "tblFile");

// Get newFileID
if( !dbRow.IsNull("FileID") )
nFileID = (int)dbRow["FileID"];

// Close connection
dbConn.Close();

// Return FileID
return nFileID;
}
寫入庫。
private void ShowTheFile(int FileID)
{
// Define SQL select statement
string SQL = "SELECT FileSize, FileData, ContentType FROM tblFile WHERE FileID = "
+ FileID.ToString();

// Create Connection object
OleDbConnection dbConn = new OleDbConnection(GetConnectionString());

// Create Command Object
OleDbCommand dbComm = new OleDbCommand(SQL, dbConn);

// Open Connection
dbConn.Open();

// Execute command and receive DataReader
OleDbDataReader dbRead = dbComm.ExecuteReader();

// Read row
dbRead.Read();

// Clear Response buffer
Response.Clear();

// Set ContentType to the ContentType of our file
Response.ContentType = (string)dbRead["ContentType"];

// Write data out of database into Output Stream
Response.OutputStream.Write((byte[])dbRead["FileData"], 0, (int)dbRead["FileSize"]);

// Close database connection
dbConn.Close();

// End the page
Response.End();
}
讀出

5. 文件轉二進制流保存到資料庫中是不是比較節省資料庫空間

以二進制方式保存主要是考慮資料庫欄位存儲大小的問題,及方便性,不見得節資料庫空間。

6. 資料庫中的二進制文件如何還原並保存

文件存庫裡面啊?那多佔空間啊!載入是很慢的!是不是圖片?
這里有個操作物理路徑下載的函數:
private void FileDown(string strPath)
{
System.IO.FileInfo file = new System.IO.FileInfo(strPath);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.FullName, System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
else
{
ClientScript.RegisterStartupScript(GetType(), "", "<script language='javascript'>alert('文件不存在!');</script>");
}
}
如果是存取圖片的話:

存:
FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write);
byte[] myData = new byte[fileStream.Length];
fileStream.Read(myData, 0, System.Convert.ToInt32(fileStream.Length));//從流中讀取位元組塊,並將數據寫入到該緩沖區
fileStream.Close();
...把mydata插入到資料庫就好勒!

取:
取出byte[]來,直接image.formStream(fs);

7. 如何實現將文件以二進制形式存放到資料庫中

這個很簡單的,這要把表單
這樣設置一下,表單裡面的數據就是以二進制的形式傳到資料庫的,至於怎麼傳到資料庫,這個就不用說吧,一個SQL插入語句就行了的,。

8. 資料庫中二進制數據主要是用來存儲什麼數據的麻煩舉例說說

很多資料庫將存儲二進制的欄位都定義為BLOB類型,實際上它可以存儲任何數據類型,比如數字型或者是字元型,當然了,資料庫中已經有數字型和字元型自己的定義,所以我們一般也不會將這些數據存儲在BLOB中,在BLOB中最常存儲的數據是文件數據,原因是因為BLOB有個特點,那就是存入的數據長度不受什麼限制,明白了吧?如果你想保存一張圖片在資料庫中,那麼BLOB類型的欄位是你唯一的選擇。

9. 如何將二進制文件存入Oracle資料庫中

先把文件讀取到內存,再以二進制格式保持到資料庫中的大欄位中(clob或clob)。
寫大對象。

Java code

public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
OutputStream os = null;
FileInputStream fis = null;
int bs = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","---");
conn.setAutoCommit(false);
stat = conn.createStatement();
stat.executeUpdate("insert into t_video(id,video) values(1,empty_blob())");

rs = stat.executeQuery("select video from t_video where id = 1");
rs.next();
oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1);
os = blo.getBinaryOutputStream();
bs = blo.getBufferSize();
fis = new FileInputStream("D:\\Temp\\MPlayer-CVS-20040808-K&K\\mplayer.exe");
byte[] buf = new byte[bs];
int length = 0;

while(true)
{
length = fis.read(buf);
if(length == -1) break;
os.write(buf,0,length);
}

os.close();
os = null;
fis.close();
fis = null;
conn.commit();
conn.setAutoCommit(true);
conn.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}

讀大對象

Java code

InputStream is = null;
FileOutputStream fos = null;
byte[] buf = null;
int bs = 0;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","-");
conn.setAutoCommit(false);
stat = conn.createStatement();

rs = stat.executeQuery("select video from t_video where id = 1");
rs.next();
oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1);
bs = blo.getBufferSize();
buf = new byte[bs];
int length = 0;
is = blo.getBinaryStream();
fos = new FileOutputStream("d:\\test.exe");

while(true) {
length = is.read(buf);
if(length == -1) break;
fos.write(buf,0,length);
}

fos.close();
fos = null;
is.close();
is = null;
conn.commit();
conn.setAutoCommit(true);
conn.close();
...

10. 如何將txt,doc等文件以二進制形式保存到資料庫和從資料庫讀出

用文件流的方式,把從文件中讀出的數據轉換成二進制,從資料庫中讀出就是反方向的:
private void button1_Click(object sender, EventArgs e)
{
byte[] buffer;
buffer = File.ReadAllBytes("readme.doc"); //讀取文件內容

//創建連接
SqlConnection connect = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BSPlatform2008;Data Source=.\SqlExpress");
SqlCommand cmd = connect.CreateCommand();
cmd.CommandText = "INSERT INTO Tmp (FileContent) VALUES (@FileContent)"; //FileContent欄位是Image類型
cmd.Parameters.Add("@FileContent", SqlDbType.Image);
cmd.Parameters["@FileContent"].Value = buffer; //接受byte[]類型的值
connect.Open();
cmd.ExecuteNonQuery();
connect.Close();
}

熱點內容
華為如何用電腦解鎖手機密碼 發布:2024-05-04 14:54:38 瀏覽:574
斐波那契數列的遞歸演算法 發布:2024-05-04 14:34:55 瀏覽:969
數字支付密碼哪裡找 發布:2024-05-04 14:26:50 瀏覽:86
天翼雲免費存儲 發布:2024-05-04 14:22:55 瀏覽:782
微信56g緩存怎麼解決 發布:2024-05-04 14:09:41 瀏覽:707
sqlupdatewhereand 發布:2024-05-04 13:55:47 瀏覽:586
java視頻教程推薦 發布:2024-05-04 13:55:08 瀏覽:86
安卓官服閃耀暖暖怎麼換 發布:2024-05-04 13:46:37 瀏覽:171
我的世界精靈伺服器怎麼抓寵物 發布:2024-05-04 13:28:54 瀏覽:960
編譯androidwebkit 發布:2024-05-04 13:11:37 瀏覽:761