110,043
社区成员




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace datapicture
{
public partial class Form1 : Form
{
DataSet Mydataset;
SqlConnection myconn;
SqlDataAdapter mydr;
public Form1()
{
InitializeComponent();
}
private void ShowDbImage()
{
byte[] bytes=(byte[]) Mydataset.Tables[0].Rows[this.listBox1.SelectedIndex][1];
MemoryStream memStream = new MemoryStream(bytes);
try
{
Bitmap MyImage = new Bitmap(memStream);
this.pictureBox1.Image = MyImage;
}
catch
{
MessageBox.Show(this, "读取数据库中的图像信息失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.pictureBox1.Image = null;
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.myconn = new SqlConnection("server=localhost;database=pubs;Uid=sa;Pwd=sa");
this.mydr = new SqlDataAdapter("select * from pub_info", "server=localhost;database=pubs;Uid=sa;Pwd=sa");
SqlCommandBuilder mybu = new SqlCommandBuilder(this.mydr);
this.mydr.UpdateCommand = mybu.GetUpdateCommand();
this.Mydataset = new DataSet();
this.mydr.Fill(this.Mydataset,"pub_info");
this.textBox1.DataBindings.Add(new Binding("text", this.Mydataset, "pub_info.pr_info"));
for (int i = 0; i < this.Mydataset.Tables[0].Rows.Count; i++)
{
this.listBox1.Items.Add(this.Mydataset.Tables[0].Rows[i][0]);
}
this.listBox1.SetSelected(0,true);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog myFileDialog = new OpenFileDialog();
myFileDialog.ShowDialog();
if (myFileDialog.FileName.Trim() != "")
{
Stream mystream = myFileDialog.OpenFile();
int length = (int)mystream.Length;
byte[] bytes = new byte[length];
mystream.Read(bytes,0,length);
mystream.Close();
this.Mydataset.Tables[0].Rows[this.listBox1.SelectedIndex][1] = bytes;
ShowDbImage();
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
byte[] bytes = System.Text.Encoding.Unicode.GetBytes("");
int iIndex = this.listBox1.SelectedIndex;
this.Mydataset.Tables[0].Rows[iIndex][1] = bytes;
this.listBox1.SetSelected(iIndex + 1, true);
ShowDbImage();
}
catch
{
MessageBox.Show("删除完毕!","Info",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void button3_Click(object sender, EventArgs e)
{
this.mydr.Update(this.Mydataset,"pub_info");
MessageBox.Show(this, "保存数据库中的图像信息成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ShowDbImage();
this.BindingContext[this.Mydataset, "pub_info"].Position = this.listBox1.SelectedIndex;
}
}
}