日期:2009-10-03 浏览次数:20400 次
列名 | 类型 | 目的 |
ID | Integer | 主键ID |
IMGTITLE | Varchar(50) | 图片的标题 |
IMGTYPE | Varchar(50) | 图片类型. ASP.NET要以辨认的类型 |
IMGDATA | Image | 用于存储二进制数据 |
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(); } |
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Data.SqlClient; namespace WindowsApplication21 { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; private string ConnectionString = "Integrated Security=SSPI;Initial Catalog=;Data Source=localhost;"; private SqlConnection conn = null;
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
|