.NET中如何循环遍历一个文件夹中的图片并将图片以二进制的方式存入sqlserver数据库
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace upload
{
public partial class Form1 : Form
{
string[] st;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//打开念团山文件夹路径,并显示所有文件
if (string.IsNullOrEmpty(textBox1.Text))
{
this.errorProvider1.SetError(this.textBox1, "必须输入文件夹地址");
return;
}
st=Directory.GetFiles(textBox1.Text);
string[] st1 = new string[st.Length];
int i = 0;
foreach (var item in st)
{
st1[i++] = Path.GetFileName(item);
}
listBox1.DataSource = st1;
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
//将符合后缀的文件保存到数据库
if (string.IsNullOrEmpty(textBox2.Text))
{
this.errorProvider1.SetError(this.textBox2, "必须输入后缀");
return;
}
foreach (var item in st)
{
string st3=Path.GetExtension(item);
if (textBox2.Text.Contains(st3.Substring(1)))
{
byte[] bt=new byte[File.OpenRead(item).Length];
File.OpenRead(item).Read(bt,0,bt.Length);
//连接数据库字符串
using (SqlConnection conn = new SqlConnection("Data Source=" + textBox3.Text + ";Initial Catalog=" + textBox4.Text + ";User Id=" + textBox5.Text + ";Password=" + textBox6.Text)) {
//sql命令 小程序 不值当的写3层了 直接上字符串
string sql = "insert into " + textBox7.Text + " (name,img) values (@Name,@Img)";
SqlCommand cmd = new SqlCommand(sql, conn);
或源 cmd.Parameters.AddRange(new SqlParameter[]{
仔中 new SqlParameter("@Name",Path.GetFileNameWithoutExtension(item)),
new SqlParameter("@Img",bt)
});
cmd.ExecuteNonQuery();
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//获得数据库中保存的信息
using (SqlConnection conn = new SqlConnection("Data Source=" + textBox3.Text + ";Initial Catalog=" + textBox4.Text + ";User Id=" + textBox5.Text + ";Password=" + textBox6.Text)) {
DataTable st = new DataTable();
string sql = "select id,name from " + textBox7.Text;
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
adapter.Fill(st);
dataGridView2.DataSource = st;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
//选中列表中的一条数据,从数据库中获得图片,并显示
using (SqlConnection conn = new SqlConnection("Data Source=" + textBox3.Text + ";Initial Catalog=" + textBox4.Text + ";User Id=" + textBox5.Text + ";Password=" + textBox6.Text)) {
string sql = "select img from " + textBox7.Text+" where id="+dataGridView2.SelectedRows[0].Cells["id"].Value;
SqlCommand cmd=new SqlCommand(sql,conn);
conn.Open();
byte[] bt=cmd.ExecuteScalar() as byte[];
MemoryStream ms = new MemoryStream();
ms.Write(bt, 0, bt.Length);
Image img= Image.FromStream(ms);
int height=pictureBox1.Height;
int width =(int)(img.Width*1.0/img.Height*height);
Image img1=new Bitmap(width,height);
Graphics gs = Graphics.FromImage(img1);
gs.DrawImage(img,0,0,width,height);
pictureBox1.Image = img1;
}
}
}
}