📄 databaseimage.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Data;
namespace Full
{
/// <summary>
/// DatabaseImage 的摘要说明。
/// </summary>
public class DatabaseImage : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btnBrower;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnClose;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
DataSet ds;
SqlDataAdapter da;
public DatabaseImage()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnBrower = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "选择图象";
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 40);
this.listBox1.Name = "listBox1";
this.listBox1.ScrollAlwaysVisible = true;
this.listBox1.Size = new System.Drawing.Size(72, 160);
this.listBox1.TabIndex = 1;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox1.Location = new System.Drawing.Point(80, 40);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(408, 160);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 216);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(576, 120);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "textBox1";
//
// btnBrower
//
this.btnBrower.Location = new System.Drawing.Point(504, 40);
this.btnBrower.Name = "btnBrower";
this.btnBrower.TabIndex = 4;
this.btnBrower.Text = "浏览";
this.btnBrower.Click += new System.EventHandler(this.btnBrower_Click);
//
// btnDelete
//
this.btnDelete.Location = new System.Drawing.Point(504, 72);
this.btnDelete.Name = "btnDelete";
this.btnDelete.TabIndex = 5;
this.btnDelete.Text = "删除";
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(504, 104);
this.btnSave.Name = "btnSave";
this.btnSave.TabIndex = 6;
this.btnSave.Text = "保存";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(504, 136);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 7;
this.btnClose.Text = "关闭";
//
// DatabaseImage
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(584, 341);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnBrower);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label1);
this.Name = "DatabaseImage";
this.Text = "DatabaseImage";
this.Load += new System.EventHandler(this.DatabaseImage_Load);
this.ResumeLayout(false);
}
#endregion
private void DatabaseImage_Load(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=pubs;user=sa;pwd=");
conn.Open();
da = new SqlDataAdapter("select * from pub_info",conn);
SqlCommandBuilder myBuilder = new SqlCommandBuilder(da);
da.UpdateCommand = myBuilder.GetUpdateCommand();
ds = new DataSet();
da.Fill(ds,"pub_info");
this.textBox1.DataBindings.Add(new Binding("Text",ds,"pub_info.pr_info"));
for(int i = 0;i < ds.Tables[0].Rows.Count;i++)
{
this.listBox1.Items.Add(ds.Tables[0].Rows[i][0]);
}
this.listBox1.SetSelected(0,true);
}
private void btnBrower_Click(object sender, System.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();
ds.Tables[0].Rows[this.listBox1.SelectedIndex][1] = bytes;
ShowDBImage();
}
}
public void ShowDBImage()
{
try
{
byte[] bytes = (byte[])ds.Tables[0].Rows[this.listBox1.SelectedIndex][1];
MemoryStream memStream = new MemoryStream(bytes);
Bitmap myImage = new Bitmap(memStream);
this.pictureBox1.Image = myImage;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnSave_Click(object sender, System.EventArgs e)
{
da.Update(ds,"pub_info");
MessageBox.Show("保存成功");
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ShowDBImage();
this.BindingContext[ds,"pub_info"].Position = this.listBox1.SelectedIndex;
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
byte[] bytes = System.Text.Encoding.Unicode.GetBytes("");
int onindex = this.listBox1.SelectedIndex;
ds.Tables[0].Rows[onindex][1] = bytes;
this.listBox1.SetSelected(onindex + 1,true);
ShowDBImage();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -