2011-03-01 56 views
0

我没有将图像制作成二进制形式,但现在想从数据库中获取图像数据。 所以你可以给出这样的想法。 我已经做了类似下面插入图片:如何从C#中的数据库中获取图像?

FileDialog dialog = new OpenFileDialog(); 
dialog.InitialDirectory = @":D\"; 
dialog.Filter = "(*.jpg;*.gif;*.jpeg;*.bmp)| *.jpg; *.gif; *.jpeg; *.bmp"; 
if (dialog.ShowDialog() == DialogResult.OK) 
{ 
    imagename = dialog.FileName; 
    pictureBox1.Image = Image.FromFile(imagename); 
} 
dialog = null; 

那么它也存储在数据库中,但现在我必须以检索在明年形成图像我该怎么办?

+0

图像几乎总是二进制形式。如果您要将图像存储在数据库中,则几乎可以确定将其存储为二进制文件。你的问题不是很清楚。你可以更具体一些,也许给一些代码示例? – 2011-03-01 06:18:43

+0

图像如何存储在数据库中(代码示例更可取)? – 2011-03-01 06:18:57

+1

“我没有把图像变成二进制形式” - 所以; **它是如何存储在数据库中的?什么字段类型?以及您使用的是什么数据访问工具? – 2011-03-01 06:19:11

回答

1
int O_id =Convert.ToInt32(textBox2.Text); 

      SqlConnection cn = new SqlConnection(strCn); 
      SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData, O_id) VALUES (@BLOBData,'"+O_id+"')", cn); 
      String strBLOBFilePath = textBox1.Text;//Modify this path as needed. 


      //Read jpg into file stream, and from there into Byte array. 
      FileStream fsBLOBFile = new FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read); 
      Byte[] bytBLOBData = new Byte[fsBLOBFile.Length]; 
      fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length); 
      fsBLOBFile.Close(); 

      //Create parameter for insert command and add to SqlCommand object. 
      SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false, 
         0, 0, null, DataRowVersion.Current, bytBLOBData); 
      cmd.Parameters.Add(prm); 

      //Open connection, execute query, and close connection. 
      cn.Open(); 
      cmd.ExecuteNonQuery(); 
      MessageBox.Show("Picture has been uploaded"); 
      cn.Close(); 
+0

如果您打算用更多信息更新您的问题,您可以选择这样做。这篇文章是否回答你的问题? – Mizipzor 2012-03-20 09:24:38

+0

是的,它适合我。 – devilsmind 2012-03-20 09:27:37

0

OK,假设你存储在图像数据库中的BLOB字段,下面的代码检索BLOB字段的数据,创建一个内存流和内存流加载Bitmap

using (SqlConnection conn = ...) 
{ 
    conn.Open(); 

    using (SqlCommand cmd = new SqlCommand("SELECT Picture FROM <tableName> WHERE ...", conn) 
    using (SqlDataReader reader = cmd.ExecuteReader()) 
    { 
     if (reader.Read()) 
     { 
      byte[] picData= reader["Picture"] as byte[] ?? null; 

      if (picData!= null) 
      { 
       using (MemoryStream ms = new MemoryStream(picData)) 
       { 
        // Load the image from the memory stream. How you do it depends 
        // on whether you're using Windows Forms or WPF. 
        // For Windows Forms you could write: 
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms); 
       } 
      } 
     } 
    } 
} 
2
protected void butSubmit_Click(object sender, EventArgs e) 
{ 
SqlConnection connection = null; 
try 
{ 
Byte[] imgByte = null; 
    if (FileUpload1.HasFile && FileUpload1.PostedFile != null) 
{ 
HttpPostedFile File = FileUpload1.PostedFile; 
imgByte = new Byte[File.ContentLength]; 
File.InputStream.Read(imgByte, 0, File.ContentLength); 
} 
connection = new SqlConnection(ConfigurationManager.ConnectionStrings   
"ConnectionString"].ConnectionString.ToString()); 

connection.Open(); 
string sql = "INSERT INTO Table1(title,image) VALUES(@theTitle, @theImage) SELECT  
@@IDENTITY"; 
SqlCommand cmd = new SqlCommand(sql, connection); 
cmd.Parameters.AddWithValue("@theTitle", txtTitle.Text); 
cmd.Parameters.AddWithValue("@theImage", imgByte); 
int id = Convert.ToInt32(cmd.ExecuteScalar()); 
lblStatus.Text = String.Format("ID is {0}", id); 

Image1.ImageUrl = "~/DisplayImg.ashx?id=" + id; 
} 
{ 
lblStatus.Text = "There was an error"; 
} 
finally 
{ 
connection.Close(); 
} 

}

0
using System.Data.SqlClient; 


using System.Drawing; 

using System.Data; 

using System.IO; 

using System.Drawing.Imaging; 


public void Save_Image(Object sender, EventArgs e) 
{ 

    // Create a byte[] from the input file 

    int len = Upload.PostedFile.ContentLength; 
    byte[] pic = new byte[len]; 
    Upload.PostedFile.InputStream.Read (pic, 0, len); 

    // Insert the image into the database 

    SqlConnection connection = new 
SqlConnection (@"server=abc\.SQLEXPRESS;database=Storage;uid=sa;pwd=sa"); 

    try 
    { 
     connection.Open(); 
     SqlCommand cmd = new SqlCommand ("insert into Image " 
      + "(Picture) values (@pic)", connection); 

     cmd.Parameters.Add ("@pic", pic); 
     cmd.ExecuteNonQuery(); 

    } 
    finally 
    { 
     connection.Close(); 
    } 
} 

我们只存储字节的图像到表名称 “图片” 其中只包含一列。 将字节图像存储到数据库中消耗大量数据库大小 用于大图像存储和从数据库检索特定图像 使用搜索需要较长的时间进行处理。这会导致 性能低下和存储问题。

+1

我想下面的一个不是代码,所以修改它.. – devilsmind 2012-03-21 05:17:02

相关问题