当前位置:首页 » 操作系统 » 数据库二进制文件保存

数据库二进制文件保存

发布时间: 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-18 14:08:28 浏览:819
路由器的密码最多是多少位 发布:2024-05-18 13:58:18 浏览:419
扫描服务器名称如何填 发布:2024-05-18 13:36:29 浏览:114
芒果缓存的视频看不了视频怎么下载不了 发布:2024-05-18 13:35:14 浏览:519
c语言发短信 发布:2024-05-18 13:23:08 浏览:833
vb数据库程序 发布:2024-05-18 13:01:57 浏览:111
新建文件夹2免费手机 发布:2024-05-18 12:56:13 浏览:365
自己在家搭建服务器有水冷散热吗 发布:2024-05-18 12:47:27 浏览:649
旧版的安卓手机怎么使用微信 发布:2024-05-18 12:46:36 浏览:467
我的世界服务器开多久 发布:2024-05-18 12:45:32 浏览:593