c#如何把图片,文本以二进制写入oracle数据库
展开全部
上传
//新增文件之-----------存文件到数据库
public void attach(string filename,string fn,string username,string title )
{
Users.Class1 u=new Users.Class1();//得到用户ID
int uid = u.getUserID(username);
Papers.papers p = new papers();
int paper_id=p.getpaperid(title, uid);//得到论文ID
string sql = "update paper_table set attach=:aa,attachfilename=:bb where ID='"+paper_id+"'";
OracleCommand cmd = new OracleCommand(sql, conn);
conn.Open();
FileStream fs = File.OpenRead(filename);
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
cmd.Parameters.Add("aa", System.Data.OracleClient.OracleType.BFile, b.Length).Value = b;
cmd.Parameters.Add("bb", System.Data.OracleClient.OracleType.VarChar).Value = fn;
cmd.ExecuteNonQuery();
conn.Close();
}
下载
先把下载好的二进制转换成以前存过数据库的格式,即刚存的时候要保存文件的扩展名,下载还原的时候用.
转换后的文件夹在临时的文件夹,后来直接response.redirect("*.*")就可以下载了.
//下载文件-收集于网上 只是注解-以下是针对Oracle数据库,SQL server一样
public string download(string pid,string path)
{
FileStream objFs;
BinaryWriter objBw;
int bufferSize = 260000;//这个是缓冲区大小,设置太小,小于下载的文件大小则会出错
byte [] outByte = new byte[bufferSize];
long retval;
long startIndex = 0;
string fileName = "";
string sql = "select * from paper_table where id='" + pid + "'";//从数据库取
OracleCommand cmd = new OracleCommand(sql, conn);
conn.Open();
OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
fileName = dr["attachfilename"].ToString();//文件名
string tempfilename = fileName;
fileName =path+fileName;
objFs = new FileStream(fileName, FileMode.OpenOrCreate,FileAccess.Write);
objBw = new BinaryWriter(objFs);
startIndex = 0;
retval = dr.GetBytes(8, startIndex, outByte, 0, bufferSize);
while ( retval == bufferSize)
{
objBw.Write(outByte);
objBw.Flush();
startIndex += bufferSize;
retval = dr.GetBytes(8, startIndex, outByte, 0, bufferSize);
}
objBw.Write(outByte, 0, (int)retval -1);
objBw.Flush();
objBw.Close();
objFs.Close();
dr.Close();
conn.Close();
return tempfilename;
}
//新增文件之-----------存文件到数据库
public void attach(string filename,string fn,string username,string title )
{
Users.Class1 u=new Users.Class1();//得到用户ID
int uid = u.getUserID(username);
Papers.papers p = new papers();
int paper_id=p.getpaperid(title, uid);//得到论文ID
string sql = "update paper_table set attach=:aa,attachfilename=:bb where ID='"+paper_id+"'";
OracleCommand cmd = new OracleCommand(sql, conn);
conn.Open();
FileStream fs = File.OpenRead(filename);
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
cmd.Parameters.Add("aa", System.Data.OracleClient.OracleType.BFile, b.Length).Value = b;
cmd.Parameters.Add("bb", System.Data.OracleClient.OracleType.VarChar).Value = fn;
cmd.ExecuteNonQuery();
conn.Close();
}
下载
先把下载好的二进制转换成以前存过数据库的格式,即刚存的时候要保存文件的扩展名,下载还原的时候用.
转换后的文件夹在临时的文件夹,后来直接response.redirect("*.*")就可以下载了.
//下载文件-收集于网上 只是注解-以下是针对Oracle数据库,SQL server一样
public string download(string pid,string path)
{
FileStream objFs;
BinaryWriter objBw;
int bufferSize = 260000;//这个是缓冲区大小,设置太小,小于下载的文件大小则会出错
byte [] outByte = new byte[bufferSize];
long retval;
long startIndex = 0;
string fileName = "";
string sql = "select * from paper_table where id='" + pid + "'";//从数据库取
OracleCommand cmd = new OracleCommand(sql, conn);
conn.Open();
OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
fileName = dr["attachfilename"].ToString();//文件名
string tempfilename = fileName;
fileName =path+fileName;
objFs = new FileStream(fileName, FileMode.OpenOrCreate,FileAccess.Write);
objBw = new BinaryWriter(objFs);
startIndex = 0;
retval = dr.GetBytes(8, startIndex, outByte, 0, bufferSize);
while ( retval == bufferSize)
{
objBw.Write(outByte);
objBw.Flush();
startIndex += bufferSize;
retval = dr.GetBytes(8, startIndex, outByte, 0, bufferSize);
}
objBw.Write(outByte, 0, (int)retval -1);
objBw.Flush();
objBw.Close();
objFs.Close();
dr.Close();
conn.Close();
return tempfilename;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询