c#中对文件的操作小结
2005-04-18 15:00
441 查看
c#中对文件的操作小结
1、建立一个文本文件
public class FileClass
2、读文件
public class FileClass
3、追加操作
public class FileClass
4、下载文件(From DataBase Read)
int FileID=Convert.ToInt32(DataList_Event_AttachFile.DataKeys[e.Item.ItemIndex]);
string sql="select FileName,File_Data,File_Type from AttachFile where FileID="+FileID;
Amcham.Data d=new Amcham.Data();
Amcham.Data.DataResult ds=new Amcham.Data.DataResult();
ds=d.GetSearchResult(sql);
if (ds.dr.Read())
byte[] Buffer=new byte[ds.dr.GetSqlBinary(1).Length];
ds.dr.GetBytes(1,0,Buffer,0,Buffer.Length);
Response.ContentType=ds.dr.GetSqlString(2).ToString();
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + ds.dr.GetSqlString(0).ToString() + "\"");
Response.BinaryWrite(Buffer);
Response.End();//避免把页面HTML代码写入文件尾部
5、读附件入数据库
FileStream=file.PostedFile.InputStream;
FileLen=file.PostedFile.ContentLength;
FileName=Path.GetFileName(file.PostedFile.FileName);
FileType=file.PostedFile.ContentType;
byte[] FileContent=new byte[FileLen];
FileStream.Read(FileContent,0,FileLen);
AttachFile.FileName=FileName;
AttachFile.File_data=FileContent;
AttachFile.FileType=FileType;
AttachFile.EventID=EventID;
AttachFile.AddAttach();
public class AttachFile
public System.Data.SqlTypes.SqlInt32 AttachID;
public System.Data.SqlTypes.SqlInt32 EventID;
public System.Data.SqlTypes.SqlInt32 SpeakerID;
public System.Data.SqlTypes.SqlString FileName;
public System.Data.SqlTypes.SqlString FileType;
public byte[] File_data;
public int AddAttach()
string InsertSql="INSERT INTO AttachFile(FileID, EventID, SpeakerID, FileName,File_Type, File_data) VALUES (@" +
"FileID, @EventID, @SpeakerID, @FileName,@FileType, @File_data)";
SqlConnection myConnection = new SqlConnection(amcham.Data.ConDB.ConnectionString);
SqlCommand myCommand = new SqlCommand(InsertSql, myConnection);
// Add Parameters
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileID", System.Data.SqlDbType.Int));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EventID", System.Data.SqlDbType.Int));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SpeakerID", System.Data.SqlDbType.Int));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileName", System.Data.SqlDbType.VarChar, 50));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@File_data", System.Data.SqlDbType.Image));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileType", System.Data.SqlDbType.VarChar, 50));
// Add Value
this.AttachID=this.GetNewAttachID();
myCommand.Parameters["@FileID"].Value=this.AttachID;
myCommand.Parameters["@EventID"].Value=this.EventID;
myCommand.Parameters["@SpeakerID"].Value=this.SpeakerID;
myCommand.Parameters["@FileName"].Value=this.FileName;
myCommand.Parameters["@File_data"].Value=this.File_data;
myCommand.Parameters["@FileType"].Value=this.FileType;
// Execute the command
try
// Open the connection and execute the Command
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
return (int)(this.AttachID);
}
catch (Exception e)
// An error occurred, pass the exception up Event
throw e;
}
}
1、建立一个文本文件
public class FileClass
2、读文件
public class FileClass
3、追加操作
public class FileClass
4、下载文件(From DataBase Read)
int FileID=Convert.ToInt32(DataList_Event_AttachFile.DataKeys[e.Item.ItemIndex]);
string sql="select FileName,File_Data,File_Type from AttachFile where FileID="+FileID;
Amcham.Data d=new Amcham.Data();
Amcham.Data.DataResult ds=new Amcham.Data.DataResult();
ds=d.GetSearchResult(sql);
if (ds.dr.Read())
byte[] Buffer=new byte[ds.dr.GetSqlBinary(1).Length];
ds.dr.GetBytes(1,0,Buffer,0,Buffer.Length);
Response.ContentType=ds.dr.GetSqlString(2).ToString();
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + ds.dr.GetSqlString(0).ToString() + "\"");
Response.BinaryWrite(Buffer);
Response.End();//避免把页面HTML代码写入文件尾部
5、读附件入数据库
FileStream=file.PostedFile.InputStream;
FileLen=file.PostedFile.ContentLength;
FileName=Path.GetFileName(file.PostedFile.FileName);
FileType=file.PostedFile.ContentType;
byte[] FileContent=new byte[FileLen];
FileStream.Read(FileContent,0,FileLen);
AttachFile.FileName=FileName;
AttachFile.File_data=FileContent;
AttachFile.FileType=FileType;
AttachFile.EventID=EventID;
AttachFile.AddAttach();
public class AttachFile
public System.Data.SqlTypes.SqlInt32 AttachID;
public System.Data.SqlTypes.SqlInt32 EventID;
public System.Data.SqlTypes.SqlInt32 SpeakerID;
public System.Data.SqlTypes.SqlString FileName;
public System.Data.SqlTypes.SqlString FileType;
public byte[] File_data;
public int AddAttach()
string InsertSql="INSERT INTO AttachFile(FileID, EventID, SpeakerID, FileName,File_Type, File_data) VALUES (@" +
"FileID, @EventID, @SpeakerID, @FileName,@FileType, @File_data)";
SqlConnection myConnection = new SqlConnection(amcham.Data.ConDB.ConnectionString);
SqlCommand myCommand = new SqlCommand(InsertSql, myConnection);
// Add Parameters
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileID", System.Data.SqlDbType.Int));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EventID", System.Data.SqlDbType.Int));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SpeakerID", System.Data.SqlDbType.Int));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileName", System.Data.SqlDbType.VarChar, 50));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@File_data", System.Data.SqlDbType.Image));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileType", System.Data.SqlDbType.VarChar, 50));
// Add Value
this.AttachID=this.GetNewAttachID();
myCommand.Parameters["@FileID"].Value=this.AttachID;
myCommand.Parameters["@EventID"].Value=this.EventID;
myCommand.Parameters["@SpeakerID"].Value=this.SpeakerID;
myCommand.Parameters["@FileName"].Value=this.FileName;
myCommand.Parameters["@File_data"].Value=this.File_data;
myCommand.Parameters["@FileType"].Value=this.FileType;
// Execute the command
try
// Open the connection and execute the Command
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
return (int)(this.AttachID);
}
catch (Exception e)
// An error occurred, pass the exception up Event
throw e;
}
}
相关文章推荐
- C# 文件操作小结
- c#中对文件的操作小结(http://www.a2605.org/ddvipcom/program/c-/index1/134.htm)
- C#文件的操作小结
- c#中对文件的操作小结
- c#中对文件的操作小结
- c#中对文件的操作小结
- C# 对文件的操作小结
- C#对文件操作小结
- C#中对文件的操作小结
- c#中对文件的操作小结
- C#中文件操作的小结 由于有时候用到就忘了,所以记录一下,以便下次用到
- c#中对文件的操作小结
- c#中对文件的操作小结
- C# 文件操作相关
- C# 文件操作
- C# 获取当前文件、文件夹的路径及操作环境变量
- C#操作Access的一些小结
- C# 文件操作
- C#读文件,转成Json,写文件操作,例子
- C# 文件与目录的基本操作(System.IO)