比如D:\2007.dat 文件是一个用户定义二进制文件,怎么在SQL Server的image字段读写呢?

解决方案 »

  1.   

    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=185154
    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=6404
      

  2.   

    int temp=Int32.Parse(this.FileUp.PostedFile.InputStream.Length.ToString());
    byte[] FileC;
    FileC=new Byte[this.FileUp.PostedFile.InputStream.Length];
    FileUp.PostedFile.InputStream.Read(FileC, 0,temp); string FName=FileUp.PostedFile.FileName;
    FName=FName.Substring(FName.LastIndexOf("\\")+1); myExeSQL.Cmd .Parameters .Add ("@content",System.Data.SqlDbType.Image);    
    myExeSQL.Cmd.Parameters ["@content"].Value=FileC; string InsterSQL="INSERT INTO resource(title,filename,keyword,type,sender,sub_id,fit_user,source,profile,format,length,makers,content) VALUES ('";
    InsterSQL+=this.Ttitle .Text+"','";
    InsterSQL+=FName+"','";
    InsterSQL+=this.Tkeyword .Text+"','";
    InsterSQL+=this.Dsource_kind .SelectedItem .Value+"','";
    InsterSQL+=Session["username"].ToString ()+"','";
    InsterSQL+=this.Dsubject .SelectedItem .Value+"','";
    InsterSQL+=this.Dfit_user .SelectedItem .Value +"','";
    InsterSQL+=this.Tsource .Text+"','";
    InsterSQL+=this.Tprofile .Text+"','";
    InsterSQL+=FileUp.PostedFile.ContentType+"','";
    InsterSQL+=temp+"','";
    InsterSQL+=this.Tauthor .Text+"',";
    InsterSQL+="@content)"; myExeSQL.ExeSQLAction (InsterSQL);
      

  3.   

    string strSQL="select * from resource where resource_id="+ID; string strConn=GetConnectionString();
    SqlConnection myConnection=new SqlConnection(strConn);
    myConnection.Open(); SqlCommand myCommand=new SqlCommand(strSQL,myConnection); SqlDataReader myReader = myCommand.ExecuteReader();
    if(myReader.Read())
    {
    Response.ContentType=myReader["format"].ToString(); //告诉IE文件的MIME类型
    // Response.AddHeader ("Content-Disposition","attachment;filename="+myReader["filename"] );
    //byte[] Buffer=new Byte[myReader.GetInt16(3)];
    //byte Buffer=myReader.GetValue(1);
    //Response.BinaryWrite(Buffer);
    Response.OutputStream.Write((byte[])myReader["content"],  0, (int)myReader["length"]);           }
      

  4.   

    放入数据库要使用参数,取出来直接用byte[]转换(如:((byte[])myReader["content"])
      

  5.   

    明白啦。就是存的时候先把文件转成byte[],然后赋值给定义的image参数,最后运行SQL命令进行提交。
    取的时候把image列转成byte[]类型的,然后赋值给byte[]类型的变量。