如何将图像保存到数据类型为varchar(100)的sql server并检索该数据? [英] how to save image to sql server whose data type is varchar(100) and retrive that data?
问题描述
我正在工作的公司有一个IBM SERVER,我想将图像上传到该服务器并在gridview中显示我正在使用MS sql server management 2008所以我想知道如何将图像上传到该服务器服务器并在发布网站后将其重新发送到bcz,图像剂量会显示在gridview中,所以请帮助我!!
这是我的代码用于插入特定文件夹但不插入服务器
protected void btnSubmit_Click( object sender,EventArgs e)
{
int id = Convert.ToInt32(Session [ AgentMasterID]);
string temp = ;
if (fuProducImage.HasFile)
{
if (CheckFileType ()== true )
{
string ext1 = Path.GetExtension( fuProducImage.FileName);
var query =( from c in agb.AgentProducts orderby c.AgentProductID 降序 选择 c.AgentProductID)。first();
int lastid = Convert.ToInt32(query);
int last = lastid + 1 ;
string directoryPath = Server.MapPath( ../ AgentImages /)+ id;
string vitualp = directoryPath.Replace( @ C:\ Users \ NET DEVELOPMENT \Desktop\working\AgentWeb, 〜)。替换( @ \, /);
string filepath = vitualp + / 跨度>;
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(HttpContext.Current.Server .MapPath(filepath + /));
fuProducImage.SaveAs(HttpContext.Current.Server.MapPath(filepath + / + last + ext1));
temp = filepath + last + ext1;
}
else
{
fuProducImage.SaveAs(Server.MapPath(filepath + last + ext1));
temp = filepath + last + ext1;
}
// fuProducImage.SaveAs(MapPath(〜/ AgentImages /+ fuProducImage。 FileName));
}
}
dmp.Product = txtProduct.Text;
dmp.AgentMasterID = Convert.ToInt32(会话[ AgentMasterID]);
dmp.Description = txtDescription.Text;
dmp.Features = txtFeatures.Text;
dmp.ProductMRP = Convert.ToDecimal(txtMRP.Text);
dmp.ProductProfit = Convert.ToDecimal(txtProfit.Text);
dmp.ProductSellingPrice = Convert.ToDecimal(txtSellingprice.Text);
dmp.ProductImage = temp;
// if(!IsPostBack)
// {
dmp.InsertAgentproduct();
// }
lblPimage.Visible = 真跨度>;
lblPimage.Text = 数据插入成功;
displayproduct();
txtProduct.Text = ;
txtProfit.Text = ;
txtMRP.Text = ;
txtFeatures.Text = ;
txtSellingprice.Text = ;
txtDescription.Text = ;
}
bool CheckFileType()
{
string ext1 = Path.GetExtension(fuProducImage.FileName);
switch (ext1.ToLower())
{
case 。png:
return true ;
case 。jpg:
return true ;
case .jpeg:
return true ;
默认:
返回 false 跨度>;
}
}
我想插入服务器以便在发布后网站应该在gridview中显示!!请帮助
差不多,你不能 - 图像的100个字符非常小:即使它是100个字节,32bit颜色只有5乘5像素!
你可以做什么(也可能是你应该做的)是把路径放到图像上数据库中的文件(虽然100个字符对于完整路径来说有点短,但对于大多数人来说已经足够了)然后在需要显示它时通过路径访问图像数据。
您需要一种不同类型的列来存储表数据(BLOB类型) - http:/ /msdn.microsoft.com/en-us/library/bb895234.aspx [ ^ ]。
鉴于长度只有100个字符,这里的一个解决方案是存储路径文件而不是文件本身。
the company in which i am working have an IBM SERVER, i want to upload image to that server and display it in gridview i am using MS sql server management 2008 so i want to know how to upload image to that server and retrive it fom there bcz after publishing the website the image dosent get displayed in the gridview so help me on that!!
this is my code for inserting in a particular folder but not in server
protected void btnSubmit_Click(object sender, EventArgs e) { int id = Convert.ToInt32(Session["AgentMasterID"]); string temp = ""; if (fuProducImage.HasFile) { if (CheckFileType() == true) { string ext1 = Path.GetExtension(fuProducImage.FileName); var query = (from c in agb.AgentProducts orderby c.AgentProductID descending select c.AgentProductID).First(); int lastid = Convert.ToInt32(query); int last = lastid + 1; string directoryPath = Server.MapPath("../AgentImages/") + id; string vitualp = directoryPath.Replace(@"C:\Users\NET DEVELOPMENT\Desktop\working\AgentWeb", "~").Replace(@"\", "/"); string filepath = vitualp + "/"; if (!Directory.Exists(filepath)) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filepath + "/")); fuProducImage.SaveAs(HttpContext.Current.Server.MapPath(filepath + "/" + last + ext1)); temp = filepath + last + ext1; } else { fuProducImage.SaveAs(Server.MapPath(filepath + last + ext1)); temp = filepath + last + ext1; } //fuProducImage.SaveAs(MapPath("~/AgentImages/" + fuProducImage.FileName)); } } dmp.Product = txtProduct.Text; dmp.AgentMasterID = Convert.ToInt32(Session["AgentMasterID"]); dmp.Description = txtDescription.Text; dmp.Features = txtFeatures.Text; dmp.ProductMRP = Convert.ToDecimal(txtMRP.Text); dmp.ProductProfit = Convert.ToDecimal(txtProfit.Text); dmp.ProductSellingPrice = Convert.ToDecimal(txtSellingprice.Text); dmp.ProductImage = temp; //if (!IsPostBack) //{ dmp.InsertAgentproduct(); //} lblPimage.Visible = true; lblPimage.Text = "Data Inserted Sucesfully"; displayproduct(); txtProduct.Text = ""; txtProfit.Text = ""; txtMRP.Text = ""; txtFeatures.Text = ""; txtSellingprice.Text = ""; txtDescription.Text = ""; } bool CheckFileType() { string ext1 = Path.GetExtension(fuProducImage.FileName); switch (ext1.ToLower()) { case ".png": return true; case ".jpg": return true; case ".jpeg": return true; default: return false; } }
i want to insert into server so that after publishing the website it should display it in gridview!! pls help解决方案Pretty much, you can't - 100 characters is very small for an image: even if it was 100 bytes, that would only be 5 by 5 pixels at 32bit colour!
What you can do (and probably what you are supposed to do) is put the path to the image file in the database (though 100 chars is a bit short for a "full path" it will be enough for most of them) and then access the image data via the path when you need to display it.
You will need a different type of column to store table data (BLOB type) - http://msdn.microsoft.com/en-us/library/bb895234.aspx[^].
Given that the length is only 100 characters, one solution here would be to store the path of the file instead of the file itself.
这篇关于如何将图像保存到数据类型为varchar(100)的sql server并检索该数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!