Uploading and retrieving images from SQL Server
Link: http://www.dotnetbips.com/displayarticle.aspx?id=60
Link: http://www.dotnetbips.com/displayarticle.aspx?id=60
解决方案 »
- 急!在线等,有答案立刻结贴。
- ASP.NET的MVC具体是分层?
- gridview自动分页时 只有一页时,怎么显示页脚?如何显示共几页 当前第几页,上一页,下一页
- 这个三目运算怎么写?速度结贴
- DropDownList 默认值的问题~~请高手帮忙看看
- ASP.NET中能不能像在form中使用datagridview那样修改数据,每一个单元格都需要修改,然后一次性提交?
- 问个超简单的问题,我忘记了.
- 希望能够得到帮忙
- 如何在APS.NET页面中通过HTTPWebRequest访问另外一个页面,同时保持原来的Session状态。
- 通过点击不同的按钮 来显示不同的层,怎么写呀?
- 讨论一下:程序员35后干什么?顺便散分。
- 如何用shell命令调用一个exe文件?
// private void Button1_Click(object sender, System.EventArgs e)
// {
// SqlConnection con = new SqlConnection("Server=yxg;uid=sa;pwd=;database=northwind");
// SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
// SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
// DataSet ds = new DataSet("MyImages");
//
// da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// FileStream fs = new FileStream(@"E:\winnt\Gone Fishing.jpg", FileMode.OpenOrCreate, FileAccess.Read);
//
// byte[] MyData= new byte[fs.Length];
// fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
//
// fs.Close();
//
// da.Fill(ds,"MyImages");
//
// DataRow myRow;
// myRow=ds.Tables["MyImages"].NewRow();
//
// myRow["Description"] = "This would be description text";
// myRow["imgField"] = MyData;
// ds.Tables["MyImages"].Rows.Add(myRow);
// da.Update(ds, "MyImages");
//
// con.Close();
// }
//
// private void Button2_Click(object sender, System.EventArgs e)
// {
// SqlConnection con = new SqlConnection("Server=yxg;uid=sa;pwd=;database=northwind");
// SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages",con);
// SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
// DataSet ds = new DataSet("MyImages");
//
// byte[] MyData= new byte[0];
//
// da.Fill(ds,"MyImages");
// DataRow myRow;
// myRow=ds.Tables["MyImages"].Rows[0];
//
// MyData=(byte[])myRow["imgField"];
// int ArraySize = new int();
// ArraySize = MyData.GetUpperBound(0);
//
// FileStream fs = new FileStream(@"E:\winnt\Gone Fishing2.jpg", FileMode.OpenOrCreate, FileAccess.Write);
// fs.Write(MyData, 0,ArraySize);
// fs.Close();
// }
{
if (File1.PostedFile != null)
{
try
{
//File1.PostedFile.SaveAs("c:\\"+TextBox1.Text);
//fs.Read(imgData, 0, fs.Length);
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("server=ljj;uid=sa;pwd=;database=testcdd");
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();
}
catch (Exception exc)
{
TextBox1.Text=exc.Message+exc.InnerException+exc.HelpLink ;
}
} }private void Button2_Click(object sender, System.EventArgs e)
{
string s=Request.QueryString["TextBox1"];
string imgid =Request.QueryString["imgid"];
// string connstr=((NameValueCollection)
// Context.GetConfig("appSettings"))["connstr"];
SqlConnection connection = new SqlConnection("server=ljj;uid=sa;pwd=;database=testcdd");
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"] );
Response.End();
}
connection.Close(); }