SoFunction
Updated on 2025-04-09

Summary of basic methods for operating local files and saving files to databases

Namespace:

using ;

Write text files

StreamWriter sw=("c:\\");  
("C#"); //Write a line of text(""); //Write a line of text at the end of the text();                    //Clear();                    //closure

Read text files

StreamReader sr = ("c:\\"); 
();                  //Read a row of data();                    //Read a character();                  //Read from the current location until the end of text();                    //Release resources

Append text file

StreamWriter sw = ("c:\\"); 
("C#"); //Write a line of text(""); //Write a line of text at the end of the text();                     //Clear();                     //closure

Determine whether the file exists

("c:\\"); 

Delete files

("c:\\"); 

Copy the file

("c:\\", "c:\\");   //Copy c:\\ to c:\\\

Move files

("c:\\", "d:\\");  //Move c:\\ to d:\\\

Folder creation, movement, and deletion

("c:\\test");       //Delete the test folder under the C drive("c:\\test");  //Create a test folder on the C drive("c:\\test");       //Verify whether the C disk test folder exists("c:\\test", "d:\\test");  //Move c:\test to d:\test

Save files in Oracle database (C#)
Oracle has Blobs and Clobs that can save large amounts of data. Among them, Blob refers to a binary large object, which is the abbreviation of the English Binary Large Object, which is used to store a large amount of binary data. Clob means that large character objects are abbreviated in English Character Large Object, which is used to store large amounts of text data.
1. Database table

-- Create a file table

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 
); 

 
--Set the tb_file primary key to increase automatically

create sequence seq_tb_file  --Create a self-increasing sequence 
minvalue 1 
maxvalue 9999999999999999999 
start with 1 
increment by 1 
nocache 
order; 
 
create or replace trigger ai_tb_file --Create a trigger,Self-increment key when inserting a new recordid 
before insert on tb_file 
for each row 
when ( is null) 
begin 
select seq_tb_file.nextval into : from dual; 
end; 

2 Save and read binary files in the database

//Add namespaceusing System; 
using ; 
using ; 
using ; 
 
/// <summary> 
/// Insert the file into the database/// </summary> 
/// <param name="filePath">File name, containing the path, such as c:\</param>/// &lt;returns&gt;&lt;/returns&gt; 
public int Insert(string filePath) 
{ 
  string connStr = "data source=orcl;user=mydbuser;password=mydbuser;"; 
  OracleConnection conn = new OracleConnection(connStr); 
  OracleCommand cmd = (); 
 
  //Read the file  FileStream fs = (filePath); 
  byte[] buffer = new byte[]; 
  (buffer, 0, ); 
 
  OracleParameter paramFileContent = (); 
   = ; 
   = "FileContent"; 
   = ; 
   = buffer; 
  (paramFileContent); 
 
  OracleParameter paramFileName = (); 
   = ; 
   = "FileName"; 
   = ; 
   = (filePath); 
  (paramFileName); 
 
  string sqlInsert = "insert into tb_file (file_name, file_content) values (:FileName, :FileContent)"; 
   = sqlInsert; 
   = ; 
 
  (); 
  int result = (); 
  (); 
 
  return result; 
} 
 
 
/// &lt;summary&gt; 
/// Get file from database according to file name/// &lt;/summary&gt; 
/// <param name="fileName">File name in the database</param>/// <param name="savePath">The save path of the file, including the file name, such as c:\</param>public void Select(string fileName, string savePath) 
{ 
  string connStr = "data source=orcl;user=mydbuser;password=mydbuser;"; 
  OracleConnection conn = new OracleConnection(connStr); 
  OracleCommand cmd = (); 
 
  string sqlSelect = "select file_name, file_content from tb_file where file_name=:FileName"; 
   = sqlSelect; 
   = ; 
 
  OracleParameter paramFileName = (); 
   = ; 
   = "FileName"; 
   = ; 
   = fileName; 
  (paramFileName);       
 
  (); 
  OracleDataReader dr = (); 
  (); 
  byte[] buffer = (byte[])dr["file_content"]; 
  (); 
  (); 
 
  //Save the file to the specified path  (savePath, buffer); 
}