怎么向access中存放和读取图片(c#写代码,不是存放路径) 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!--引用地址:http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=185154-->求救!asp.net+c#上传图片到access数据库及显示图片的代码?主要解答者:redbb提交人:redbb感谢:sjc0、redbb、xhzuo审核者:webdiyer社区对应贴子:查看 A : 不要VB.net和带SQL Server存储过程的方法,用不上哦! --------------------------------------------------------------- 我用了存储过程不过你改改 Stream stream = insertimg.PostedFile.InputStream; byte[] buffer = new byte[insertimg.PostedFile.ContentLength]; int aa = stream.Read(buffer, 0, (int)stream.Length); string filename = System.IO.Path.GetFileNameWithoutExtension(insertimg.PostedFile.FileName.ToString()); string expandname = Path.GetExtension(insertimg.PostedFile.FileName.ToString()); int size = (int)stream.Length; stream.Read(buffer, 0, size); 你把buffer传入你的image字段就可以了! --------------------------------------------------------------- store it : 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(); --------------------------------------------------------------- show it : 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://dotnet.aspx.cc/article/9154bc99-df64-4e2d-b096-26c99ce464be/read.aspx { OpenFileDialog openfile = new OpenFileDialog(); openfile.InitialDirectory = "c:\\"; openfile.Filter = "All files (*.*)|*.*"; openfile.FilterIndex = 1; openfile.RestoreDirectory = true; String FileName = ""; if (openfile.ShowDialog() == DialogResult.OK) { FileName = openfile.FileName; } FileStream objFileStream; StreamReader objStreamReader; string strLine = ""; objFileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read); byte[] fileData = new byte[objFileStream.Length]; //把文件流填充到数组 objFileStream.Read(fileData, 0, fileData.Length); connection.updateSqlforImage(" update sb set picture=" + "@Image where id=2", fileData); objFileStream.Close(); } DataTable dt=connection.selectSql("select picture from sb where id=2"); Byte[] byteBLOBData = new Byte[0]; byteBLOBData = (Byte[])dt.Rows[0][0]; MemoryStream stmBLOBData = new MemoryStream(byteBLOBData); pictureBox1.Image = Image.FromStream(stmBLOBData); 求一正则错误 c# winform中webbrowser中层加载问题 这种情况下能用泛型吗 请问如何在后台判断GridView中是否为空? 一个sunstring的问题. 关于devexpress 中 grid 单元格编辑 竟然出错了!再求一次。100分求助--如何得到axWebbrowser中用户所选择的文字? 在线!如何将日期的月和日组合成字符串?我是初学者 aspx中没有全局变量,如何实现以下的功能?? 请教字符串加密解密的问题。急!! 请告诉我自动根据C#源文件生成项目的工具名称好吗? 请问vs 2005中的Crystal Reports水晶报表的版本是多少啊?
引用地址:http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=185154
-->求救!asp.net+c#上传图片到access数据库及显示图片的代码?
主要解答者:redbb提交人:redbb
感谢:sjc0、redbb、xhzuo
审核者:webdiyer社区对应贴子:查看
A : 不要VB.net和带SQL Server存储过程的方法,用不上哦!
---------------------------------------------------------------
我用了存储过程不过你改改
Stream stream = insertimg.PostedFile.InputStream;
byte[] buffer = new byte[insertimg.PostedFile.ContentLength];
int aa = stream.Read(buffer, 0, (int)stream.Length);
string filename = System.IO.Path.GetFileNameWithoutExtension(insertimg.PostedFile.FileName.ToString());
string expandname = Path.GetExtension(insertimg.PostedFile.FileName.ToString());
int size = (int)stream.Length;
stream.Read(buffer, 0, size);
你把buffer传入你的image字段就可以了!
---------------------------------------------------------------
store it :
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();
---------------------------------------------------------------
show it :
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();
}
OpenFileDialog openfile = new OpenFileDialog();
openfile.InitialDirectory = "c:\\";
openfile.Filter = "All files (*.*)|*.*";
openfile.FilterIndex = 1;
openfile.RestoreDirectory = true;
String FileName = "";
if (openfile.ShowDialog() == DialogResult.OK)
{
FileName = openfile.FileName; }
FileStream objFileStream;
StreamReader objStreamReader;
string strLine = ""; objFileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
byte[] fileData = new byte[objFileStream.Length];
//把文件流填充到数组
objFileStream.Read(fileData, 0, fileData.Length);
connection.updateSqlforImage(" update sb set picture=" + "@Image where id=2", fileData);
objFileStream.Close();
}
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])dt.Rows[0][0];
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image = Image.FromStream(stmBLOBData);