C#操作本地⽂件及保存⽂件到数据库的基本⽅法总结命名空间:
using System.IO;
写⽂本⽂件
StreamWriter sw=File.CreateText("c:\\");
sw.WriteLine("C#"); //写⼊⼀⾏⽂本
sw.Write("www.csdn"); //在⽂本末尾写⼊⽂本
sw.Flush(); //清空
sw.Close(); //关闭
读⽂本⽂件
StreamReader sr = File.OpenText("c:\\");
sr.ReadLine(); //读取⼀⾏数据
sr.Read(); //读取⼀个字符
sr.ReadToEnd(); //从当前位置读取到⽂本结束
sr.Close(); //释放资源
追加⽂本⽂件
StreamWriter sw = File.AppendText("c:\\");
sw.WriteLine("C#"); //写⼊⼀⾏⽂本
sw.Write("www.csdn"); //在⽂本末尾写⼊⽂本
sw.Flush(); //清空
sw.Close(); //关闭
判断⽂件是否存在
File.Exists("c:\\");
删除⽂件
File.Delete("c:\\");
复制⽂件
File.Copy("c:\\", "c:\\"); //把c:\\复制到c:\\
移动⽂件
File.Copy("c:\\", "d:\\"); //把c:\\移动到d:\\
⽂件夹创建、移动、删除
Directory.Delete("c:\\test"); //删除C盘下的test⽂件夹
Directory.CreateDirectory("c:\\test"); //在C盘创建test⽂件夹
Directory.Exists("c:\\test"); //验证C盘test⽂件夹是否存在
Directory.Move("c:\\test", "d:\\test"); //把c:\test移动到d:\test
Oracle数据库中保存⽂件(C#)Oracle中有Blob和Clob可以保存⼤数据量。其中Blob是指⼆进制⼤对象也就是英⽂Binary Large Object的缩写,⽤来存储⼤量⼆进制数据。⽽Clob是指⼤字符对象是英⽂Character Large Object的缩写,⽤来存储⼤量⽂本数据。
1.数据库表
-- 创建⽂件表
create table tb_file
(
id number(20) not null,
file_name nvarchar2(100),
file_content blob,
constraint pk_tb_file primary key (id)
)
tablespace mydb storage(
initial 64K
minextents 1
maxextents unlimited
);
--设置tb_file主键⾃增
create sequence seq_tb_file --创建⾃增序列
minvalue 1
maxvalue 9999999999999999999
start with 1
increment by 1
nocache
order;
create or replace trigger ai_tb_file --创建触发器,当插⼊新记录时⾃增主键id
before insert on tb_file
for each row
when (new.id is null)
begin
select seq_val into :new.id from dual;
end;
2 在数据库中保存和读取⼆进制⽂件
//添加命名空间
using System;
using System.Data.OracleClient;
using System.IO;
using System.Data;
/// <summary>
/// 把⽂件插⼊数据库中
/// </summary>
/// <param name="filePath">⽂件名,包含路径,如c:\temp.rar</param>
/// <returns></returns>
public int Insert(string filePath)
{
string connStr = "data source=orcl;user=mydbuser;password=mydbuser;";
OracleConnection conn = new OracleConnection(connStr);
OracleCommand cmd = conn.CreateCommand();
//读取⽂件
FileStream fs = File.OpenRead(filePath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
OracleParameter paramFileContent = cmd.CreateParameter();
paramFileContent.OracleType = OracleType.Blob;
paramFileContent.ParameterName = "FileContent";
paramFileContent.Direction = ParameterDirection.Input;
paramFileContent.Value = buffer;
cmd.Parameters.Add(paramFileContent);
OracleParameter paramFileName = cmd.CreateParameter();
paramFileName.OracleType = OracleType.VarChar;
paramFileName.ParameterName = "FileName";
paramFileName.Direction = ParameterDirection.Input;
paramFileName.Value = Path.GetFileName(filePath);
cmd.Parameters.Add(paramFileName);
string sqlInsert = "insert into tb_file (file_name, file_content) values (:FileName, :FileContent)"; cmd.CommandText = sqlInsert;
cmd.CommandType = CommandType.Text;
conn.Open();
int result = cmd.ExecuteNonQuery();
conn.Close();
return result;
}
/// <summary>
/// 根据⽂件名从数据库中获取⽂件
/// </summary>
/// <param name="fileName">数据库中的⽂件名</param>
/// <param name="savePath">⽂件的保存路径,包括⽂件名,如c:\file.rar</param>
public void Select(string fileName, string savePath)
{
getsavefilename
string connStr = "data source=orcl;user=mydbuser;password=mydbuser;";
OracleConnection conn = new OracleConnection(connStr);
OracleCommand cmd = conn.CreateCommand();
string sqlSelect = "select file_name, file_content from tb_file where file_name=:FileName"; cmd.CommandText = sqlSelect;
cmd.CommandType = CommandType.Text;
OracleParameter paramFileName = cmd.CreateParameter();
paramFileName.OracleType = OracleType.VarChar;
paramFileName.ParameterName = "FileName";
paramFileName.Direction = ParameterDirection.Input;
paramFileName.Value = fileName;
cmd.Parameters.Add(paramFileName);
conn.Open();
OracleDataReader dr = cmd.ExecuteReader();
dr.Read();
byte[] buffer = (byte[])dr["file_content"];
dr.Close();
conn.Close();
//把⽂件保存到指定路径
File.WriteAllBytes(savePath, buffer);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论