column name datatype purpose id integer identity column primary key imgtitle varchar(50) stores some user friendly title to identity the image imgtype varchar(50) stores image content type. this will be same as recognized content types of asp.net imgdata image stores actual image or binary data.
stream imgdatastream = file1.postedfile.inputstream; int imgdatalen = file1.postedfile.contentlength; string imgtype = file1.postedfile.contenttype; string imgtitle = textbox1.text; byte[] imgdata = new byte[imgdatalen]; int n = imgdatastream.read(imgdata,0,imgdatalen); string connstr= ((namevaluecollection)context.getconfig ("appsettings"))["connstr"]; sqlconnection connection = new sqlconnection(connstr); sqlcommand command = new sqlcommand ("insert into imagestore(imgtitle,imgtype,imgdata) values ( @imgtitle, @imgtype,@imgdata )", connection ); sqlparameter paramtitle = new sqlparameter ("@imgtitle", sqldbtype.varchar,50 ); paramtitle.value = imgtitle; command.parameters.add( paramtitle); sqlparameter paramdata = new sqlparameter ( "@imgdata", sqldbtype.image ); paramdata.value = imgdata; command.parameters.add( paramdata ); sqlparameter paramtype = new sqlparameter ( "@imgtype", sqldbtype.varchar,50 ); paramtype.value = imgtype; command.parameters.add( paramtype ); connection.open(); int numrowsaffected = command.executenonquery(); connection.close();
private void page_load(object sender, system.eventargs e) { string imgid =request.querystring["imgid"]; string connstr=((namevaluecollection) context.getconfig("appsettings"))["connstr"]; string sql="select imgdata, imgtype from imagestore where id = " + imgid; sqlconnection connection = new sqlconnection(connstr); sqlcommand command = new sqlcommand(sql, connection); connection.open(); sqldatareader dr = command.executereader(); if(dr.read()) { response.contenttype = dr["imgtype"].tostring(); response.binarywrite( (byte[]) dr["imgdata"] ); } connection.close(); }
新闻热点
疑难解答