图片存库测试【读文有感】
1
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8
using System.Data.SqlClient;9
using System.IO;10

11
namespace ImageForDataBase12


{13
public partial class Form1 : Form14

{15
public Form1()16

{17
InitializeComponent();18
}19

20
private void dataGridBind()21

{22
SqlConnection con = new SqlConnection(@"server=TONGBIN\SQLSERVER2005;database=Test;uid=sa;pwd=123456;");23
con.Open();24
string sql = "select ImgID,ImageContentType,ImageSize,ImageData from ImageStore";25
SqlDataAdapter sdr = new SqlDataAdapter(sql, con);26
DataSet ds = new DataSet();27
sdr.Fill(ds,"TT");28
this.grdMain.DataSource = ds.Tables["TT"];29
con.Close();30
}31

32
private void btnUpLoad_Click(object sender, EventArgs e)33

{34
SqlConnection con = new SqlConnection(@"server=TONGBIN\SQLSERVER2005;database=Test;uid=sa;pwd=123456;");35
con.Open();36
string strFileName = string.Empty;37
if (openFileDialog1.ShowDialog() != DialogResult.OK)38

{39
return;40
}41
strFileName = openFileDialog1.FileName;42
File.GetAttributes(strFileName);43
File.GetLastWriteTime(strFileName);44
45
FileStream fs = File.Open(strFileName, FileMode.Open);46
byte[] by = new byte[fs.Length];47
fs.Read(by, 0, (int)fs.Length);48
String SqlCmd = "insert into ImageStore(ImgID,ImageData,ImageContentType,ImageDescription,ImageSize) values(@ImgID,@Image,@fileType,@fileDec,@fileSize)";49
SqlCommand CmdObj = new SqlCommand(SqlCmd, con);50
CmdObj.Parameters.Add("@Image", SqlDbType.Image, (int)fs.Length).Value = by;51

CmdObj.Parameters.Add("@ImgID", SqlDbType.VarChar, 20).Value = Path.GetFileName(openFileDialog1.FileName).Split(new Char[]
{ '.' })[0];52

CmdObj.Parameters.Add("@fileType", SqlDbType.VarChar, 20).Value = Path.GetFileName(openFileDialog1.FileName).Split(new Char[]
{ '.' })[1];53
CmdObj.Parameters.Add("@fileDec", SqlDbType.VarChar, 200).Value =Path.GetFileName(openFileDialog1.FileName);54
CmdObj.Parameters.Add("@fileSize", SqlDbType.VarChar).Value = "gfdgdgd";55
fs.Close();56
CmdObj.ExecuteNonQuery();57
dataGridBind();58
}59

60
private void Form1_Load(object sender, EventArgs e)61

{62
dataGridBind();63
}64

65
private void ShowImgToPic(string strImageID)66

{67
try68

{69
if (strImageID.Trim()==string.Empty)70

{71
return;72
}73
SqlConnection myConnection = new SqlConnection(@"server=TONGBIN\SQLSERVER2005;database=Test;uid=sa;pwd=123456;");74
myConnection.Open();75
string strSql="Select ImageContentType, ImageData from ImageStore Where ImgID="+"'"+strImageID.ToString()+"'";76
SqlDataAdapter sdr = new SqlDataAdapter(strSql, myConnection);77
DataSet ds = new DataSet();78
sdr.Fill(ds, "gggg");79
byte[] by = (byte[])ds.Tables["gggg"].Rows[0]["ImageData"];80
MemoryStream memStream = new MemoryStream(by);81
Image img = new Bitmap(memStream);82
picShow.Image = img;83
myConnection.Close();84
}85
catch (SqlException SQLexc)86

{87
MessageBox.Show("提取数据时出现错误:" + SQLexc.ToString());88
}89
}90

91
private void btnSearch_Click(object sender, EventArgs e)92

{93
ShowImgToPic("tb");94
}95

96
private void grdMain_SelectionChanged(object sender, EventArgs e)97

{98
int intRow = grdMain.CurrentRow.Index;99
grdMain.CurrentCell = grdMain[0, intRow];100
ShowImgToPic(grdMain.CurrentCell.Value.ToString());101
}102
}103
}


浙公网安备 33010602011771号