Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using System.IO;
- namespace SqlImageTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- string connectionString = "Data Source=.;Initial Catalog=Testing;Integrated Security=SSPI;";
- private void button1_Click(object sender, EventArgs e)
- {
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- conn.Open();
- SqlCommand sqlCommand = new SqlCommand("Select ID, Title, ImageData FROM Image", conn);
- SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
- DataTable table = new DataTable();
- adapter.Fill(table);
- foreach (DataRow row in table.Rows)
- {
- ListViewItem item = new ListViewItem(row["Title"].ToString());
- item.Tag = row["ImageData"];
- listView1.Items.Add(item);
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- OpenFileDialog fDialog = new OpenFileDialog();
- if (fDialog.ShowDialog() == DialogResult.OK)
- {
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- conn.Open();
- SqlCommand sqlCommand = new SqlCommand("INSERT INTO Image(Title,ImageData) VALUES(@title,@data)", conn);
- SqlParameter titleParam = new SqlParameter("title", SqlDbType.NVarChar);
- titleParam.Value = m_imageTitle.Text;
- sqlCommand.Parameters.Add(titleParam);
- SqlParameter imageParam = new SqlParameter("@data", SqlDbType.Image);
- FileStream fs = new FileStream(fDialog.FileName, FileMode.Open);
- byte[] data = new byte[fs.Length];
- fs.Read(data, 0, (int)data.Length);
- imageParam.Value = data;
- sqlCommand.Parameters.Add(imageParam);
- sqlCommand.ExecuteNonQuery();
- }
- }
- }
- private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
- {
- pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])e.Item.Tag));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement