上传的:Stream imgDataStream = u_photo.PostedFile.InputStream;
int imgDataLen = u_photo.PostedFile.ContentLength;
byte[] imgDataTemp = new byte[imgDataLen];
imgDataStream.Read( imgDataTemp, 0, imgDataLen );
strSql = "INSERT INTO UserInfo ( u_photo ) VALUES ( ? )";
myConnection.Open();
myCommand = new OleDbCommand( strSql, myConnection );
myCommand.Parameters.Add( ":u_photo", OleDbType.Binary, imgDataLen, "u_photo" ).Value = imgDataTemp;
myCommand.ExecuteNonQuery();显示:string strSql = "SELECT u_photo FROM UserInfo WHERE id = '"+ id +"'";
myConnection.Open();
myCommand = new OleDbCommand( strSql , myConnection );
myReader = myCommand.ExecuteReader();
if ( myReader.Read() )
{
byte[] btImage = ( byte[] )myReader["u_photo"];
Response.BinaryWrite( btImage );
Response.End();
}
int imgDataLen = u_photo.PostedFile.ContentLength;
byte[] imgDataTemp = new byte[imgDataLen];
imgDataStream.Read( imgDataTemp, 0, imgDataLen );
strSql = "INSERT INTO UserInfo ( u_photo ) VALUES ( ? )";
myConnection.Open();
myCommand = new OleDbCommand( strSql, myConnection );
myCommand.Parameters.Add( ":u_photo", OleDbType.Binary, imgDataLen, "u_photo" ).Value = imgDataTemp;
myCommand.ExecuteNonQuery();显示:string strSql = "SELECT u_photo FROM UserInfo WHERE id = '"+ id +"'";
myConnection.Open();
myCommand = new OleDbCommand( strSql , myConnection );
myReader = myCommand.ExecuteReader();
if ( myReader.Read() )
{
byte[] btImage = ( byte[] )myReader["u_photo"];
Response.BinaryWrite( btImage );
Response.End();
}
解决方案 »
- 在线急等:asp.net 与 sql 的触发器的问题
- 变量保存问题
- 程序做好,用vs2008发布工具发布后,在目标bin目录下,还是有很多pdb文件存在,这些可否删除掉?如何得知现在的bll是release版的?
- 怎么在后台代码中动态为<head id="id" runat="server"></head>添加<script></script>
- 哪些情况下必须使用委托才能完成需求?
- 文件路径问题
- 谁能写一个简单的三层架构!
- 怎样让treeview的结点点击事件响应啊,我想在点击treeview的结点时触发页面回调。望大家帮忙!
- 没人理我!!小问题
- 【星星们,月亮今天中刀了,大家快来救我啊】关于Http 500出错的问题
- 这是为什么???高人们 帮个忙吧
- 我做了一个用户控件,里面含一个DATAGRID,在一个ASPX页中,我引用了这个用户控件,并给那个DATAGRID添加一列“编辑”列,但是点击这一列
string filepath=server.mappath("file/"+path.getfilename(file1.postedfile.filename));建立数据库连接
sqlconnection myconnection=...
创建sqldataadapter对象,按照指定的sql语句将图片插入到数据库中
sqlcommand mycommand= new Sqlcommand("insert into aaa(title,img)") values ('"+title.text.tostring()+"','"+path.getfilename(file1.postedfile.filename)+"',myconnection;)
mycommand.connection.open();
mycommand.executenonquery();
mycommand.connection.close();
response.redirect(bbb.aspx)show.aspx<%if (dr[img].tostring()!=""){%><img src='file/<%=dr["img"] %>'><%}%>
ID Integer 主键ID
IMGTITLE Varchar(50) 图片的标题
IMGTYPE Varchar(50) 图片类型. ASP.NET要以辨认的类型
IMGDATA Image 用于存储二进制数据 2、存储图片到SQL SERVER数据库中 为了能存储到表中,你首先要上传它们到你的WEB 服务器上,你可以开发一个web form,它用来将客户端中TextBox web control中的图片入到你的WEB服务器上来。将你的 encType 属性设置为:myltipart/formdata.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(); 3、从数据库中恢复读取 现在让我们来从SQL Server中读取我们放入的数据吧!我们将要输出图片到你的浏览器上,你也可以将它存放到你要的位置。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();
}
http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
http://www.aspx.cn/article/go.asp?id=268&typeid=2
http://www.aspx.cn/article/go.asp?id=267&typeid=2
http://www.c-sharpcorner.com/Code/2002/Feb/FlashCardsMG.asp