C#,WFA,使用.NET 4.5平台,
我删除了textBox1(名字),textBox2(姓氏),pictureBox1(员工照片),button2(浏览)和button1(保存)以插入新员工。
button2->必须浏览图像并显示在pictureBox1中,
button1->必须将由button2浏览并由pictureBox1显示的图像保存到localhost中的此表中。
运行程序后,我得到此错误(找不到文件。)
(尽管我没有单独浏览的任何错误)
我只想要包含解决此WFA的代码的答案。只是想确保我对此很清楚。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SqlConnection cnn = new SqlConnection("Initial Catalog=randomcompany;Data Source=localhost;Integrated Security=SSPI;");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //Browse button
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + "All files (*.*)|*.*";
dlg.Title = "Select Employee Picture";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e) //Save button
{
try
{
cnn.Open();
string path = pictureBox1.Image.ToString();
Byte[] imagedata = File.ReadAllBytes(path);
SqlCommand cmd = new SqlCommand("INSERT INTO Employees (EmployeeFirstname, EmployeeLastname, EmployeePhoto) VALUES (@item1,@item2,@img", cnn);
cmd.Parameters.AddWithValue("@item1", textBox1.Text);
cmd.Parameters.AddWithValue("@item2", textBox2.Text);
cmd.Parameters.AddWithValue("@img", imagedata);
cmd.ExecuteNonQuery();
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
问题是:
string path = pictureBox1.Image.ToString();
不返回路径。在类字段中获取路径时将其存储。命名为_path
:
private string _path;
然后在获取文件名时进行设置:
_path = dlg.FileName;
然后在这里使用它:
Byte[] imagedata = File.ReadAllBytes(_path);
这是完整的代码修改:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string _path;
SqlConnection cnn = new SqlConnection("Initial Catalog=randomcompany;Data Source=localhost;Integrated Security=SSPI;");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //Browse button
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + "All files (*.*)|*.*";
dlg.Title = "Select Employee Picture";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
_path = dlg.FileName;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e) //Save button
{
try
{
cnn.Open();
Byte[] imagedata = File.ReadAllBytes(_path);
SqlCommand cmd = new SqlCommand("INSERT INTO Employees (EmployeeFirstname, EmployeeLastname, EmployeePhoto) VALUES (@item1,@item2,@img", cnn);
cmd.Parameters.AddWithValue("@item1", textBox1.Text);
cmd.Parameters.AddWithValue("@item2", textBox2.Text);
cmd.Parameters.AddWithValue("@img", imagedata);
cmd.ExecuteNonQuery();
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句