.net如何用二进制形式保存文件数据到数据库中?

解决方案 »

  1.   

    以下是将图片转化成二进制,其他的应该很好实现的,你参考一下
    {
            byte[] BlobData = null;
            Bitmap picture;
            FileStream stream;
            stream = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
            BlobData = new byte[stream.Length];
            stream.Read(BlobData, 0, (int)stream.Length);
            picture = new Bitmap(stream);
            stream.Close();
            return BlobData;
        }
      

  2.   

    字段定义为image类型,读取二进制文件到数据库中,则是byte[],直接赋值存储就可以了
      

  3.   

     Dim intImageSize As Int64
        Dim strImageType As String
        Dim ImageStream As Stream    ' Gets the Size of the Image
        intImageSize = PersonImage.PostedFile.ContentLength    ' Gets the Image Type
        strImageType = PersonImage.PostedFile.ContentType    ' Reads the Image
        ImageStream = PersonImage.PostedFile.InputStream    Dim ImageContent(intImageSize) As Byte
        Dim intStatus As Integer
        intStatus = ImageStream.Read(ImageContent, 0, intImageSize)    ' Create Instance of Connection and Command Object
        Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
        Dim myCommand As New SqlCommand("sp_person_isp", myConnection)    ' Mark the Command as a SPROC
        myCommand.CommandType = CommandType.StoredProcedure    ' Add Parameters to SPROC
        Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image)
        prmPersonImage.Value = ImageContent
        myCommand.Parameters.Add(prmPersonImage)    Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255)
        prmPersonImageType.Value = strImageType
        myCommand.Parameters.Add(prmPersonImageType)    Try
            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()
            Response.Write("New person successfully added!")
        Catch SQLexc As SqlException
            Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())
        End Try这个是典型的图片保存
      

  4.   

    用sql参数传入byte[]值 就可以了
      

  5.   

    基本思路无非是用BinaryReader读取文件,然后写入DB。http://www.linux-cn.com/html/program/aspnet/20070412/7006.html
    用ASP.NET2.0在数据库中存储二进制文件
      

  6.   

    byte[] p_Data= ???; 你获取的二进数据例如有个表XXX 是Image类型的 string _SqlCommand = "Insert Into XXX(MyImage) Value(@MyImage)";
                    OleDbCommand _Command = new OleDbCommand(_SqlCommand, SqlConn);    
                    _Command.Parameters.Add("@MyImage" + p_ColumnName, OleDbType.VarBinary).Value = p_Data;
                    _Command.ExecuteNonQuery();用Paramerters就可以解决了很简单的
      

  7.   

    把你要存东西先读到byte[]里面,然后传个参到Paramerter,OK