您的位置:首页 > 编程语言 > C#

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;

    }            

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: