关于上传图片,图片上传到指定的文件夹里,并将记录保存到数据库中,请问如何做啊!! 我已做了一个上传图片的功能,只是将图片保存到数据库中了!我现在不想保存到数据库中,只想把上传的路径保存到数据库里,请问如何做。如果有源码更好!!!谢谢因为本人是新手!!!!或者给个下载例题的链接。谢谢!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你提供一个上传图书信息的代码,希望对你有用。原理是先把图书信息插入数据库中,然后读出他的id号,再把你要上传的图片的名字命名为id的值。把图片的路径做一些更改,然后把路径插入到数据库中,以后直接把路径绑定到图片的url上就可以了 if ((upfile.PostedFile.ContentLength > 0) && (upfile.Value.ToString() != "")) { //将名字插入到数据库,并取出该名字对于的id,把这个id作为要上传的图片的名字 string product = this.textName.Text.ToString(); string price = this.textPrice.Text.ToString(); string content = this.textContent.Text.ToString(); price = "单价:" + price + "元"; SqlConnection con = new SqlConnection("server=.;uid=admin;pwd=pass;database=BookShopDB"); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "insert into product (product,productPrice,productNotes) values('"+product+"','"+price+"','"+content+"')"; cmd.ExecuteNonQuery(); SqlCommand dd = new SqlCommand(); dd.Connection = con; dd.CommandText = "select id from product where product='"+product+"'"; string id = Convert.ToString(dd.ExecuteScalar()); //获取上传文件的后缀名 string suffix = upfile.PostedFile.FileName.Substring(upfile.PostedFile.FileName.LastIndexOf(".")); //获取存放图片的文件夹的路径 string uploadpath = this.Server.MapPath("uploadefile"); //获取文件名 string tempFileName = upfile.PostedFile.FileName.Substring(upfile.PostedFile.FileName.LastIndexOf("\\") + 1); // ----------------------------------- //获取文件的绝对存放路径 // fileSavePath = fileSavePath.Substring(fileSavePath.LastIndexOf(":") + 1); // fileSavePath = fileSavePath.Replace("\\", "/"); // ------------------------------- //文件的存放路径 string fileSavePath = uploadpath + "\\" + id + suffix; upfile.PostedFile.SaveAs(fileSavePath); // --------------------------- fileSavePath = fileSavePath.Substring(fileSavePath.LastIndexOf(":") + 1); fileSavePath = fileSavePath.Replace("\\", "/"); //----------------------------- //将图片的路径插入到数据库中去 Response.Write(fileSavePath); SqlCommand smd = new SqlCommand(); smd.Connection = con; smd.CommandText = "update product set path= '" + fileSavePath + "'where id='" + id + "'"; smd.ExecuteNonQuery(); con.Close(); 我在网上下了好多源码程序,可以全部都是编译成dll文件的,关键的代码看不到!!!急啊!!请教!!!分不够再加!!! 保存用户登录信息 C#自动完成代码怎么用 谁有 C# 打印 DataTable 好使的代码```谢谢``!!! 请问高手,为什么js对只读的控件操作的结果反应不到后台呢 这样怎么设置相应控件的焦点 菜鸟求教:将XML数据读入数据集 C#中如何实现鼠标拖拽图片的效果? C#,新建网站出错,提示“找不到web服务器”,怎么解决呢? 这个附件怎么打开? C# 如何代码删除压缩文件中的文件 请问高手:如何获取文件路径?? 怎样判断一句话中有几个单词
{
//将名字插入到数据库,并取出该名字对于的id,把这个id作为要上传的图片的名字
string product = this.textName.Text.ToString();
string price = this.textPrice.Text.ToString();
string content = this.textContent.Text.ToString();
price = "单价:" + price + "元";
SqlConnection con = new SqlConnection("server=.;uid=admin;pwd=pass;database=BookShopDB");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into product (product,productPrice,productNotes) values('"+product+"','"+price+"','"+content+"')";
cmd.ExecuteNonQuery(); SqlCommand dd = new SqlCommand();
dd.Connection = con; dd.CommandText = "select id from product where product='"+product+"'"; string id = Convert.ToString(dd.ExecuteScalar());
//获取上传文件的后缀名
string suffix = upfile.PostedFile.FileName.Substring(upfile.PostedFile.FileName.LastIndexOf(".")); //获取存放图片的文件夹的路径
string uploadpath = this.Server.MapPath("uploadefile"); //获取文件名
string tempFileName = upfile.PostedFile.FileName.Substring(upfile.PostedFile.FileName.LastIndexOf("\\") + 1);
// -----------------------------------
//获取文件的绝对存放路径
// fileSavePath = fileSavePath.Substring(fileSavePath.LastIndexOf(":") + 1);
// fileSavePath = fileSavePath.Replace("\\", "/"); // ------------------------------- //文件的存放路径
string fileSavePath = uploadpath + "\\" + id + suffix;
upfile.PostedFile.SaveAs(fileSavePath); // --------------------------- fileSavePath = fileSavePath.Substring(fileSavePath.LastIndexOf(":") + 1);
fileSavePath = fileSavePath.Replace("\\", "/"); //-----------------------------
//将图片的路径插入到数据库中去
Response.Write(fileSavePath); SqlCommand smd = new SqlCommand();
smd.Connection = con;
smd.CommandText = "update product set path= '" + fileSavePath + "'where id='" + id + "'"; smd.ExecuteNonQuery();
con.Close();
请教!!!
分不够再加!!!