字号:大 中 小
private void button1_Click(object sender, System.EventArgs e {
string filename= textBox1.Text;//图片文件所在的路径
BinaryReader reader=null;
FileStream myfilestream = new FileStream(filename,FileMode.Open;
try {
reader=new BinaryReader(myfilestream;
byte[] image = reader.ReadBytes((intmyfilestream.Length;//存储图片到数组中。
using (SqlConnection conn = new
SqlConnection(\
{
using (SqlCommand command = conn.CreateCommand(
{
command.CommandText =@\ INTO photo(photo VALUES (@photo\
command.Parameters.Add(\
conn.Open(;
command.ExecuteNonQuery(;
conn.Close(;
MessageBox.Show(\文件保存成功!\提示\
}
}
}
catch(IOException ee {
MessageBox.Show(ee.Message.ToString(; }
finally {
if(reader!=null
reader.Close(; } }
private void button2_Click(object sender, System.EventArgs e
{//把数据表中Image字段中保存的图片信息转换图片形式在Piceture控件中显示出来。
using (SqlConnection conn = new
SqlConnection(\ {
using (SqlCommand command = conn.CreateCommand(
{
command.CommandText = @\Pid=@Pid\
command.Parameters.Add(\
conn.Open(;
byte[] imageData = (byte[]command.ExecuteScalar(;
conn.Close(;
MemoryStream mStream = new MemoryStream(;
mStream.Write(imageData, 0, imageData.Length;
mStream.Flush(;
Bitmap img= new Bitmap(mStream;
pictureBox1.Image=img;
} } }
private void button3_Click(object sender, System.EventArgs e {
openFileDialog1.ShowDialog(; }
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e {
textBox1.Text=openFileDialog1.FileName; } }