从任何文件夹将图像插入sql表,而无需使用文件上传 [英] insert image into sql table from any folder without using file upload
问题描述
将图像插入sql表时遇到问题.
列名称为 thumbnail_url ,列类型为 image ,我们必须从xml文件中插入.
但是主要的问题是在xml文件thumbnail_url中是像varchar这样的
< thumbnail_url> http://test.com/images/test.jpg</thumbnail_url>
我们为此使用了代码
We get a problem when inserting image into sql table.
columns name is thumbnail_url and column type is image and we have to insert in it from an xml file.
but the main problem is that in xml file thumbnail_url is varchar like
<thumbnail_url>http://test.com/images/test.jpg </thumbnail_url>
we have used code for that
protected void Page_Load(object sender, EventArgs e)
{
}
static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
//int i;
using (BinaryReader br = new BinaryReader(stream))
{
//i = (int)(stream.Length);
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}
static public void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}
protected void btn_Upload_Click(object sender, EventArgs e)
{
try
{
DataSet dsxml = new DataSet();
dsxml.ReadXml("C:\\Documents and Settings\\madhyam1\\Desktop\\Property XML\\propertyfeedHomecouk.xml");
string d = ConfigurationManager.AppSettings["ConnectionString"] + "";
SqlConnection sqlcon = new SqlConnection(d);
sqlcon.Open();
for (int i = 0; i <= dsxml.Tables[0].Rows.Count; i++)
//for (int i = 0; i <= 10; i++)
{
string url = dsxml.Tables[0].Rows[i][6].ToString();
if (!string.IsNullOrEmpty(url))
{
string DestinationPath = "C:/Temp";
string filename = url.Substring(url.LastIndexOf(''/'') + 1);
byte[] bytes = GetBytesFromUrl(url);
WriteBytesToFile(DestinationPath + "/" + i + ".jpg", bytes);
SqlCommand cmd = new SqlCommand(@"INSERT INTO Property_details
(price
,bedrooms
,type
,location
,postcode
,url
,thumbnail_url
,agent_id
,agent_url
,lat
,long
,developmentid
,description
,CustomerId
) VALUES(''" + dsxml.Tables[0].Rows[i][0] + "'',''" + dsxml.Tables[0].Rows[i][1] + "'',''" + dsxml.Tables[0].Rows[i][2] + "'',''" + dsxml.Tables[0].Rows[i][3] + "'',''" + dsxml.Tables[0].Rows[i][4] + "'',''" + dsxml.Tables[0].Rows[i][5] + "'',''" + dsxml.Tables[0].Rows[i][6] + "'',''" + dsxml.Tables[0].Rows[i][7] + "'',''" + dsxml.Tables[0].Rows[i][8] + "'',''" + dsxml.Tables[0].Rows[i][9] + "'',''" + dsxml.Tables[0].Rows[i][10] + "'',''" + dsxml.Tables[0].Rows[i][11] + "'',''" + dsxml.Tables[0].Rows[i][12].ToString().Replace("''", ".") + "'',0)", sqlcon);
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
通过上述代码图像被存储到C驱动器的Temp文件夹中.
从xml文件的链接开始,但
我们必须插入DATABAE.
请任何人帮助我.
谢谢.
[edit]已添加代码块-OriginalGriff [/edit]
Through the above code images is stored into Temp folder of C drive
from the link of xml file but
we have to insert into DATABAE.
Please Anyone help me.
Thanks.
[edit]Code block added - OriginalGriff[/edit]
推荐答案
大家好.
我找到了解决方案,即将提交...
欢迎使用其他替代解决方案.
hi all.
i have found a solution, that is submitted soon...
every alternate solutions are welcome.
这篇关于从任何文件夹将图像插入sql表,而无需使用文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!