2009-09-09 36 views

답변

4

여기에 게시하기 전에 약간의 인터넷 검색을 수행 읽는 얻을 수있는 null 값 initially.Use에서는 FileInfo 개체와 바이트 배열을 초기화합니다. 아래는 내가 귀하의 쿼리에 대해 얻은 세 번째 검색 결과입니다. 나는 이것을하는 방법을 안다. 그러나 그것을 다시 할 수있는 충분한 인내심이 아니다. 그래서 여기 TechArena

Function SaveImage _ 
    (ByVal FileName As String) As Integer 

    ' Create a FileInfo instance 
    ' to retrieve the files information 
    Dim fi As New FileInfo(FileName) 

    ' Open the Image file for Read 
    Dim imgStream As Stream = _ 
     fi.OpenRead 

    ' Create a Byte array the size 
    ' of the image file for the Read 
    ' methods buffer() byte array 
    Dim imgData(imgStream.Length) As Byte 
    imgStream.Read(imgData, 0, fi.Length) 

    Dim cn As New SqlConnection _ 
     (ConfigurationSettings.AppSettings("cn")) 
    Dim cmd As New SqlCommand("Images_ins", cn) 
    cmd.CommandType = CommandType.StoredProcedure 

    With cmd.Parameters 
     .Add("@FileName", VarChar, 100).Value = _ 
     fi.Name 
     .Add("@FileType", VarChar, 10).Value = + 
     fi.Extension 
     .Add("@Image", Image).Value = imgData 
     .Add("@FileSize", Int, 4).Value = fi.Length 
    End With 

    cn.Open() 
    cmd.ExecuteNonQuery() 
    cn.Close() 
    cn.Dispose() 
End Function 
1

SQL Server의 이미지 필드에서 샘플 코드는 단순히 바이트 배열입니다. 다음은 중요한 코드입니다. 데이터베이스의 이미지 필드 이름이 "imageField"라고 가정합니다. 희망이 도움이됩니다.

이미지를 검색하고 디스크에 저장하려면

''Get the image file into a Byte Array 
Dim image As Byte() = System.IO.File.ReadAllBytes("c:\image.jpg") 
''Add the byte array as a parameter to your Insert/Update SQLCommand 
parameters.Add("@ImageField", image) 
:

//dr is a DataReader returned from a SELECT command 
Dim imageInBytes As Byte() = dr("imagefield") 
Dim memoryStream As System.IO.MemoryStream = _ 
    New System.IO.MemoryStream(imageInBytes, False) 
Dim image As System.Drawing.Image = _ 
    System.Drawing.Image.FromStream(memoryStream) 
image.Save("c:\image") 

디스크에서 SQL 서버에 이미지를 저장하려면

관련 문제