Stream imgdatastream = File1.PostedFile.InputStream;www.c hinaitpower.comlOPhU7
int imgdatalen = File1.PostedFile.ContentLength;www.c hinaitpower.comlOPhU7
string imgtype = File1.PostedFile.ContentType;www.c hinaitpower.comlOPhU7
string imgtitle = TextBox1.Text;www.c hinaitpower.comlOPhU7
byte[] imgdata = new byte[imgdatalen];www.c hinaitpower.comlOPhU7
int n = imgdatastream.Read(imgdata,0,imgdatalen);www.c hinaitpower.comlOPhU7
string connstr=www.c hinaitpower.comlOPhU7
((NameValueCollection)Context.GetConfigwww.c hinaitpower.comlOPhU7
("appSettings"))["connstr"];www.c hinaitpower.comlOPhU7
SqlConnection connection = new SqlConnection(connstr);www.c hinaitpower.comlOPhU7
SqlCommand command = new SqlCommandwww.c hinaitpower.comlOPhU7
("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)www.c hinaitpower.comlOPhU7
VALUES ( @imgtitle, @imgtype,@imgdata )", connection );www.c hinaitpower.comlOPhU7
www.c hinaitpower.comlOPhU7
SqlParameter paramTitle = new SqlParameterwww.c hinaitpower.comlOPhU7
("@imgtitle", SqlDbType.VarChar,50 );www.c hinaitpower.comlOPhU7
paramTitle.Value = imgtitle;www.c hinaitpower.comlOPhU7
command.Parameters.Add( paramTitle);www.c hinaitpower.comlOPhU7
www.c hinaitpower.comlOPhU7
SqlParameter paramData = new SqlParameterwww.c hinaitpower.comlOPhU7
( "@imgdata", SqlDbType.Image );www.c hinaitpower.comlOPhU7
paramData.Value = imgdata;www.c hinaitpower.comlOPhU7
command.Parameters.Add( paramData );www.c hinaitpower.comlOPhU7
www.c hinaitpower.comlOPhU7
SqlParameter paramType = new SqlParameterwww.c hinaitpower.comlOPhU7
( "@imgtype", SqlDbType.VarChar,50 );www.c hinaitpower.comlOPhU7
paramType.Value = imgtype;www.c hinaitpower.comlOPhU7
command.Parameters.Add( paramType );www.c hinaitpower.comlOPhU7
www.c hinaitpower.comlOPhU7
connection.Open();www.c hinaitpower.comlOPhU7
int numRowsAffected = command.ExecuteNonQuery();www.c hinaitpower.comlOPhU7
connection.Close();www.c hinaitpower.comlOPhU7
private void Page_Load(object sender, System.EventArgs e)www.c hinaitpower.comlOPhU7
{www.c hinaitpower.comlOPhU7
string imgid =Request.QueryString["imgid"];www.c hinaitpower.comlOPhU7
string connstr=((NameValueCollection)www.c hinaitpower.comlOPhU7
Context.GetConfig("appSettings"))["connstr"];www.c hinaitpower.comlOPhU7
string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "www.c hinaitpower.comlOPhU7
+ imgid;www.c hinaitpower.comlOPhU7
SqlConnection connection = new SqlConnection(connstr);www.c hinaitpower.comlOPhU7
SqlCommand command = new SqlCommand(sql, connection);www.c hinaitpower.comlOPhU7
connection.Open();www.c hinaitpower.comlOPhU7
SqlDataReader dr = command.ExecuteReader();www.c hinaitpower.comlOPhU7
if(dr.Read())www.c hinaitpower.comlOPhU7
{www.c hinaitpower.comlOPhU7
Response.ContentType = dr["imgtype"].ToString();www.c hinaitpower.comlOPhU7
Response.BinaryWrite( (byte[]) dr["imgdata"] );www.c hinaitpower.comlOPhU7
}www.c hinaitpower.comlOPhU7
connection.Close();www.c hinaitpower.comlOPhU7
}www.c hinaitpower.comlOPhU7
關鍵詞: