基本信息
源码名称:图片二进制读取数据库(含数据库)
源码大小:4.33M
文件格式:.zip
开发语言:C#
更新时间:2020-09-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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
}
}