3个回答
展开全部
protected void Button1_Click(object sender, EventArgs e)
{
//上传图片
string connection = WebConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
string path = Server.MapPath("~/Picture");
string filepath = FileUpload1.PostedFile.FileName;
string name = filepath.Substring(filepath.LastIndexOf("\\")+1);//获取文件名
string Extend = filepath.Substring(filepath.LastIndexOf(".")+1);//获取格式名
if (Extend == "jpg" || Extend == "gif" || Extend == "bmp")
{
FileUpload1.SaveAs(path + "\\" + name);
string filepath1 = "~/Picture/" + name; //这里的Picture是指你要图片上传到的服务器文件夹 可以自己改
conn.Open();
string sql = "insert into Picture values('" + filepath1 + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('插入成功!')</script>");
}
else
{
Response.Write("<script>alert('图片格式不正确!')</script>");
}
}
其实你这个问题在百度很多啊 最后希望可以帮助你
{
//上传图片
string connection = WebConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
string path = Server.MapPath("~/Picture");
string filepath = FileUpload1.PostedFile.FileName;
string name = filepath.Substring(filepath.LastIndexOf("\\")+1);//获取文件名
string Extend = filepath.Substring(filepath.LastIndexOf(".")+1);//获取格式名
if (Extend == "jpg" || Extend == "gif" || Extend == "bmp")
{
FileUpload1.SaveAs(path + "\\" + name);
string filepath1 = "~/Picture/" + name; //这里的Picture是指你要图片上传到的服务器文件夹 可以自己改
conn.Open();
string sql = "insert into Picture values('" + filepath1 + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('插入成功!')</script>");
}
else
{
Response.Write("<script>alert('图片格式不正确!')</script>");
}
}
其实你这个问题在百度很多啊 最后希望可以帮助你
追问
string connection = WebConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString 什么意思
追答
这句话就是获取你配置文件的连接串 你在这里直接给连接串赋值就好
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询