如何从SQL Server获取图像

时间:2013-02-08 08:34:12

标签: image sql-server-2005

我有一个名为IMG的表格,其中包含保存图片的IDContent列。

如何从那里拍照?

我用Google搜索了这个问题,我得到的是vb.net,c#和php - mysql。

也许有人可以说什么是获得图片的最佳和最简单的方法(样本,复制/粘贴代码,程序)?

谢谢!

2 个答案:

答案 0 :(得分:1)

尝试这样的事情(来自d_r_w的回答):

SqlDataAdapter dataAdapter = new SqlDataAdapter(
   new SqlCommand("SELECT pic FROM imageTest WHERE pic_id = 1", 
     yourConnectionReference));

DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);

if (dataSet.Tables[0].Rows.Count == 1)
{
    Byte[] data = new Byte[0];
    data = (Byte[])(dataSet.Tables[0].Rows[0]["pic"]);
    MemoryStream mem = new MemoryStream(data);
    yourPictureBox.Image= Image.FromStream(mem);
} 

参加complete answer

执行此操作的更多选项:

我发现this post关于PHP但是关于MySQL(你需要更改与服务器的连接):

<?php 
// image.php - by Hermawan Haryanto &lt;hermawan@dmonster.com&gt; 
// Example PHP Script, demonstrating Storing Image in Database 
// Detailed Information can be found at http://www.codewalkers.com 

// database connection 
$conn = mysql_connect("localhost", "user", "password") 
  OR DIE (mysql_error()); 
@mysql_select_db ("hermawan", $conn) OR DIE (mysql_error()); 
$sql    = "SELECT * FROM image WHERE image_id=".$_GET["iid"]; 
$result = mysql_query ($sql, $conn); 
if (mysql_num_rows ($result)&gt;0) { 
  $row = @mysql_fetch_array ($result); 
  $image_type = $row["image_type"]; 
  $image = $row["image"]; 
  Header ("Content-type: $image_type"); 
  print $image; 
} 
?>

答案 1 :(得分:1)

经过大量的阅读和工作后找到了解决方案!

在这里! 100%工作

      Dim conn, sql, a, filename
      Dim dir 
      dir = "c:\images\" //saves files into this directory
      Dim fileObj
      Set fileObj = CreateObject("Scripting.FileSystemObject")
      Set conn = CreateObject("ADODB.Connection")
      Dim strStream
      Set strStream = CreateObject("ADODB.Stream")
      Dim rstRecordset
      Set rstRecordset = CreateObject("ADODB.Recordset")
      conn.Open "Provider=SQLOLEDB;Data Source=yourserver;Integrated Security = SSPI","username","password"
      rstRecordset.Open "Select xxx, yyy, zzz from table (nolock) where xxx = '' order by xxx desc", conn    ', adOpenKeyset, adLockOptimistic
      Set fso = CreateObject("Scripting.FileSystemObject")

      While Not rstRecordset.EOF  
     filename = rstRecordset.Fields(0) 
     er = 0

Do
    er = er+1
Loop While (fso.FileExists(dir & filename & "_" & er  & ".JPG"))  

    filename = dir & filename & "_" & er  & ".JPG"
    strStream.Type = 1
    strStream.Open
    strStream.Write rstRecordset.Fields(2).Value
    strStream.SaveToFile filename
    strStream.Close
        rstRecordset.MoveNext
    Wend

这是!