实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
namespace photo
{
public partial class Form1 : Form
{
photoclass.BaseOperate boperate = new photoclass.BaseOperate();
public static int idtrue; //判断ID
public static int sucSave=0; //批量存取, 成功存取数目
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cboxBind("select Name from 测试ptoto", "测试ptoto", "Name", comboBox1);
readImage();
}
#region 相关图像保存函数
//根据条件保存图片函数
public void SavePhoto(string pathName)
{
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);
//将图像读入到字节数组
System.IO.FileStream fs = new System.IO.FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte, 0, (int)fs.Length);
fs.Close();
fs.Dispose();
fs = null;
//查找响应项进行插入
string str = pathName.Substring(pathName.LastIndexOf("\\") 1);
string str1 = str.Substring(0, str.IndexOf("."));
//label4.Text = str1;
//建立Command命令
string M_str_sqlcon1 = "Data Source=.;Database=ptoto;Integrated Security=SSPI;";
SqlConnection conn = new SqlConnection(M_str_sqlcon1);
//SqlCommand sqlCommand1 = new SqlCommand("Insert into 测试ptoto(gradPhoto,Name) values(@img,@name) where nameId='" label4.Text "'", conn);
SqlCommand sqlCommand1 = new SqlCommand("update 测试ptoto set gradPhoto=@img,Name=@name where nameId='" str1 "'", conn);
//创建Parameter
sqlCommand1.Parameters.Add("@img", System.Data.SqlDbType.Image);
sqlCommand1.Parameters[0].Value = buffByte;
sqlCommand1.Parameters.Add("@name", System.Data.SqlDbType.VarChar);
sqlCommand1.Parameters[1].Value = pathName.Substring(pathName.LastIndexOf("\\") 1);
try
{
conn.Open();
idtrue = sqlCommand1.ExecuteNonQuery();
sucSave = idtrue;
conn.Close();
//MessageBox.Show("成功放入图片到数据库中");
}
catch (System.Exception ee)
{
MessageBox.Show(ee.Message);
}
buffByte = null;
//cboxBind("select Name from 测试ptoto", "测试ptoto", "Name", comboBox1);
}
catch (System.Exception ee)
{
// MessageBox.Show(ee.Message);
}
}
//双表关联根据条件保存图片函数
public void SavePhototwo(string pathName)
{
try
{
string str = pathName.Substring(pathName.LastIndexOf("\\") 1); //获取文件名,含后缀
string str1 = str.Substring(0, str.IndexOf(".")); //获取文件名
string sql = "select Id from 测试ptoto where nameId='" str1 "'";
// idtrue = boperate.getcom(sql); //判断是否能够找到对用ID
string sql1 = boperate.getcom1(sql); //获取ID
System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);
//将图像读入到字节数组
System.IO.FileStream fs = new System.IO.FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte, 0, (int)fs.Length);
fs.Close();
fs.Dispose();
fs = null;
//建立Command命令
string M_str_sqlcon1 = "Data Source=.;Database=ptoto;Integrated Security=SSPI;";
SqlConnection conn = new SqlConnection(M_str_sqlcon1);
//SqlCommand sqlCommand1 = new SqlCommand("Insert into 测试ptoto(gradPhoto,Name) values(@img,@name) where nameId='" label4.Text "'", conn);
SqlCommand sqlCommand1 = new SqlCommand("update photoceshi set photo=@img,name=@name where Id=" Int32.Parse(sql1) "", conn);
//创建Parameter
sqlCommand1.Parameters.Add("@img", System.Data.SqlDbType.Image);
sqlCommand1.Parameters[0].Value = buffByte;
sqlCommand1.Parameters.Add("@name", System.Data.SqlDbType.VarChar);
sqlCommand1.Parameters[1].Value = pathName.Substring(pathName.LastIndexOf("\\") 1);
try
{
conn.Open();
idtrue = sqlCommand1.ExecuteNonQuery();
sucSave = idtrue;
conn.Close();
//MessageBox.Show("成功放入图片到数据库中");
}
catch (System.Exception ee)
{
MessageBox.Show(ee.Message);
}
buffByte = null;
}
catch (System.Exception ee)
{
// MessageBox.Show(ee.Message);
}
}
//直接保存图片函数
public void SavePhoto2(string pathName)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);
//将图像读入到字节数组
System.IO.FileStream fs = new System.IO.FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte, 0, (int)fs.Length);
fs.Close();
fs = null;
//建立Command命令
string M_str_sqlcon1 = "Data Source=.;Database=ptoto;Integrated Security=SSPI;";
SqlConnection conn = new SqlConnection(M_str_sqlcon1);
SqlCommand sqlCommand1 = new SqlCommand("Insert into 测试photo1(gradPhoto,Name) values(@img,@name)", conn);
//创建Parameter
sqlCommand1.Parameters.Add("@img", System.Data.SqlDbType.Image);
sqlCommand1.Parameters[0].Value = buffByte;
sqlCommand1.Parameters.Add("@name", System.Data.SqlDbType.VarChar);
sqlCommand1.Parameters[1].Value = pathName.Substring(pathName.LastIndexOf("\\") 1);
try
{
conn.Open();
int idtrue = sqlCommand1.ExecuteNonQuery();
sucSave = idtrue;
conn.Close();
//MessageBox.Show("成功放入图片到数据库中");
}
catch (System.Exception ee)
{
MessageBox.Show(ee.Message);
}
buffByte = null;
// cboxBind("select Name from 测试ptoto", "测试ptoto", "Name", comboBox1);
}
#endregion
#region 单个存储
private void 浏览button_Click(object sender, EventArgs e)
{
this.openFileDialog1.AddExtension = false;
this.openFileDialog1.CheckFileExists = true;
this.openFileDialog1.CheckPathExists = true;
this.openFileDialog1.Multiselect = true;
this.openFileDialog1.RestoreDirectory = true;
this.openFileDialog1.Title = "图片打开对话框";
// this.openFileDialog1.SupportMultiDottedExtensions = true;
this.openFileDialog1.ValidateNames = true;
this.openFileDialog1.Filter = "image files|*.bmp;*.jpg;*.png;*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.位置textBox.Text = this.openFileDialog1.FileName;
string pathName;
pathName = this.位置textBox.Text;
// System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);
//this.pictureBox1.Image = img;
}
}
private void 保存button_Click(object sender, EventArgs e)
{
if(位置textBox.Text!="")
{
string pathName;
pathName = this.位置textBox.Text;
string str = pathName.Substring(pathName.LastIndexOf("\\") 1);
string str1 = str.Substring(0, str.IndexOf("."));
if(usercheck(str1)!=0)
{
if (MessageBox.Show("确认保存选定照片", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) !=
DialogResult.Yes)
{
return;
}
SavePhoto(pathName);
MessageBox.Show("成功放入图片到数据库中");
}
else
{
MessageBox.Show("以此身份证命名信息不存在,请核实!");
}
}
else
{
MessageBox.Show("路径不能为空");
}
}
/// <summary>
/// 对ComboBox控件进行数据绑定
/// </summary>
/// <param name="P_str_sqlstr">SQL语句</param>
/// <param name="P_str_table">表名</param>
/// <param name="P_str_tbMember">数据表中字段名</param>
/// <param name="cbox">ComboBox控件ID</param>
public void cboxBind(string P_str_sqlstr, string P_str_table, string P_str_tbMember, ComboBox cbox)
{
DataSet myds = getds(P_str_sqlstr, P_str_table);
cbox.DataSource = myds.Tables[P_str_table];
cbox.DisplayMember = P_str_tbMember;
}
/// <summary>
/// 创建一个DataSet对象
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
/// <param name="M_str_table">表名</param>
/// <returns>返回DataSet对象</returns>
public DataSet getds(string M_str_sqlstr, string M_str_table)
{
string M_str_sqlcon1 = "Data Source=.;Database=ptoto;Integrated Security=SSPI;";
SqlConnection sqlcon = new SqlConnection(M_str_sqlcon1);
SqlDataAdapter sqlda = new SqlDataAdapter(M_str_sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlda.Fill(myds, M_str_table);
return myds;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
readImage();
}
#endregion
#region 图像显示
/// <summary>
/// 读取图像
/// </summary>
public void readImage()
{
SqlConnection conn = new SqlConnection(@"Data Source=.;Database=ptoto;Integrated Security=SSPI;");
conn.Open();
SqlCommand cmd = new SqlCommand("select gradPhoto from 测试ptoto where Name='" comboBox1.Text.Trim() "'", conn);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
try
{
MemoryStream buf = new MemoryStream((byte[])reader[0]);
Image image = Image.FromStream(buf, true);
pictureBox1.Image = image;
}
catch (System.Exception ee)
{
//MessageBox.Show(ee.Message);
}
}
#endregion
#region 批量处理
private void 浏览批量button_Click(object sender, EventArgs e)
{
//openFileDialog1.FileName = "";
openFileDialog1.Filter = "image files|*.bmp;*.jpg;*.png;*.gif";
openFileDialog1.Title = "选择图像";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (string strFilename in openFileDialog1.FileNames)
{
string strname = strFilename.Substring(strFilename.LastIndexOf("\\") 1);
listView1.Items.Add(new ListViewItem(strname));
}
}
string pathNamepl = openFileDialog1.FileName;
位置批量textBox.Text = pathNamepl.Substring(0,pathNamepl.LastIndexOf("\\") 1);
}
private void 保存批量button_Click(object sender, EventArgs e)
{
//失败label.Text = "";
//成功label.Text = "";
if (listView1.CheckedItems.Count > 0 && listView1.Items.Count > 0)
{
if (MessageBox.Show("确认保存选定照片", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) !=
DialogResult.Yes)
{
return;
}
int checkCount = listView1.CheckedItems.Count;
for (int i = 0; i < this.listView1.CheckedItems.Count; i )
{
if (listView1.Items[i].Checked)
{
string str = listView1.Items[i].Text.ToString(); //获取选中项的值
string strLianJie = 位置批量textBox.Text str; //全路径
string str1 = strLianJie.Substring(strLianJie.LastIndexOf("\\") 1); //文件名,含后缀
string str2 = str1.Substring(0, str1.IndexOf(".")); //文件名,不含后缀
string strerror = 位置批量textBox.Text "未保存成功的照片";
if (usercheck(str2) != 0)
{
SavePhoto(strLianJie);
Thread.Sleep(50); //休眠50ms
}
else
{
if (Directory.Exists(strerror) == false)
{
Directory.CreateDirectory(strerror);
strerror = strerror "\\" str1;
File.Copy(strLianJie, strerror, true);
}
else
{
strerror = strerror "\\" str1;
File.Copy(strLianJie, strerror, true);
}
Thread.Sleep(50);
}
}
}
失败label.Text = (checkCount - sucSave).ToString();
成功label.Text = sucSave.ToString();
MessageBox.Show("放入图片到数据库完成");
sucSave = 0;
}
else
{
MessageBox.Show("初始未选中且不能为空!,请选取图片!");
}
listView1.Items.Clear();
}
//listView1全选操作
private void button1_Click(object sender, EventArgs e)
{
if (listView1.Items.Count!=0)
{
foreach (ListViewItem item in listView1.Items)
{
item.Checked = true;
//item.Selected = true;
}
}
else
{
MessageBox.Show("选定时不能为空,请浏览进行选择!");
}
}
private void 反选button_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
//item.Selected = false;
item.Checked=!item.Checked;
}
}
private void 全不选button_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
item.Checked = false;
}
}
#endregion
#region 关联表保存操作
//双表关联保存
private void 关联保存button_Click(object sender, EventArgs e)
{
if (位置textBox.Text != "")
{
//if (MessageBox.Show("确认保存选定照片", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) !=
// DialogResult.Yes)
//{
// return;
//}
//string pathName;
//pathName = this.位置textBox.Text;
//SavePhototwo(pathName);
//MessageBox.Show("成功放入图片到数据库中");
string pathName;
pathName = this.位置textBox.Text;
string str = pathName.Substring(pathName.LastIndexOf("\\") 1);
string str1 = str.Substring(0, str.IndexOf("."));
if (usercheck(str1) != 0)
{
if (MessageBox.Show("确认保存选定照片", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) !=
DialogResult.Yes)
{
return;
}
SavePhototwo(pathName);
if (idtrue > 0)
{
MessageBox.Show("成功放入图片到数据库中");
}
else
{
MessageBox.Show("请注意,执行不成功,无对应的ID!!!");
}
}
else
{
MessageBox.Show("以此身份证命名信息不存在,请核实!");
}
}
else
{
MessageBox.Show("路径不能为空");
}
}
//关联批量保存操作
private void 关联批量保存button_Click(object sender, EventArgs e)
{
//失败label.Text = "";
//成功label.Text = "";
if (listView1.CheckedItems.Count > 0 && listView1.Items.Count > 0)
{
if (MessageBox.Show("确认保存选定照片", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) !=
DialogResult.Yes)
{
return;
}
int checkCount = listView1.CheckedItems.Count;
for (int i = 0; i < this.listView1.CheckedItems.Count; i )
{
if (listView1.Items[i].Checked)
{
string str = listView1.Items[i].Text.ToString(); //获取选中项的值
string strLianJie = 位置批量textBox.Text str;
string str1 = strLianJie.Substring(strLianJie.LastIndexOf("\\") 1); //文件名,含后缀
string str2 = str1.Substring(0, str1.IndexOf(".")); //文件名,不含后缀
string strerror = 位置批量textBox.Text "未保存成功的关联批量照片";
if (usercheck(str2) != 0)
{
SavePhototwo(strLianJie);
Thread.Sleep(50); //休眠50ms
if (idtrue <= 0)
{
if (Directory.Exists(strerror) == false)
{
Directory.CreateDirectory(strerror);
strerror = strerror "\\" str1;
File.Copy(strLianJie, strerror, true);
}
else
{
strerror = strerror "\\" str1;
File.Copy(strLianJie, strerror, true);
}
Thread.Sleep(50);
}
}
else
{
if (Directory.Exists(strerror) == false)
{
Directory.CreateDirectory(strerror);
strerror = strerror "\\" str1;
File.Copy(strLianJie, strerror, true);
}
else
{
strerror = strerror "\\" str1;
File.Copy(strLianJie, strerror, true);
}
Thread.Sleep(50);
}
}
}
失败label.Text = (checkCount - sucSave).ToString();
成功label.Text = sucSave.ToString();
MessageBox.Show("放入图片到数据库完成");
sucSave = 0;
}
else
{
MessageBox.Show("初始未选中且不能为空!,请选取图片!");
}
listView1.Items.Clear();
}
#endregion
#region 检查身份证号是否存在
//检查身份证号是否存在
public int usercheck(string str)
{
string sql = "select * from 测试ptoto where nameId='" str "'";
SqlDataReader sqlread = boperate.getread(sql);
int i = 0;
if (sqlread.Read())
{
i = 1;
}
return i;
}
#endregion
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论