Advertisement
Guest User

Images in Sql Server

a guest
Jun 14th, 2010
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9. using System.IO;
  10.  
  11. namespace SqlImageTest
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         string connectionString = "Data Source=.;Initial Catalog=Testing;Integrated Security=SSPI;";
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.            
  24.                 using (SqlConnection conn = new SqlConnection(connectionString))
  25.                 {
  26.                     conn.Open();
  27.  
  28.                     SqlCommand sqlCommand = new SqlCommand("Select ID, Title, ImageData FROM Image", conn);
  29.                     SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
  30.                     DataTable table = new DataTable();
  31.                     adapter.Fill(table);
  32.  
  33.                     foreach (DataRow row in table.Rows)
  34.                     {
  35.                         ListViewItem item = new ListViewItem(row["Title"].ToString());
  36.                         item.Tag = row["ImageData"];
  37.                         listView1.Items.Add(item);
  38.                     }
  39.             }
  40.         }
  41.  
  42.         private void button2_Click(object sender, EventArgs e)
  43.         {
  44.             OpenFileDialog fDialog = new OpenFileDialog();
  45.             if (fDialog.ShowDialog() == DialogResult.OK)
  46.             {
  47.                 using (SqlConnection conn = new SqlConnection(connectionString))
  48.                 {
  49.                     conn.Open();
  50.  
  51.                     SqlCommand sqlCommand = new SqlCommand("INSERT INTO Image(Title,ImageData) VALUES(@title,@data)", conn);
  52.  
  53.                     SqlParameter titleParam = new SqlParameter("title", SqlDbType.NVarChar);
  54.                     titleParam.Value = m_imageTitle.Text;
  55.                     sqlCommand.Parameters.Add(titleParam);
  56.                     SqlParameter imageParam = new SqlParameter("@data", SqlDbType.Image);
  57.  
  58.                     FileStream fs = new FileStream(fDialog.FileName, FileMode.Open);
  59.                     byte[] data = new byte[fs.Length];
  60.                     fs.Read(data, 0, (int)data.Length);
  61.  
  62.                     imageParam.Value = data;
  63.  
  64.                     sqlCommand.Parameters.Add(imageParam);
  65.  
  66.                     sqlCommand.ExecuteNonQuery();
  67.                 }
  68.             }
  69.         }
  70.  
  71.         private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
  72.         {
  73.             pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])e.Item.Tag));
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement