本文介绍了使用ASP.NET C#中的gridview从文件夹和数据库中删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从数据库中删除,但我不知道如何从文件夹中删除图片。

请帮助。

为了更好地理解,请编辑我的代码并粘贴。

提前致谢。



我尝试过:



 protected void Button1_Click(object sender,EventArgs e)
{
SqlCommand command = new SqlCommand(SPinsertitem,con);
command.CommandType = CommandType.StoredProcedure;
//上传图片1 //
string strname = picture1.FileName.ToString();
strname =〜//品牌//新文件夹//+ strname;
picture1.PostedFile.SaveAs(Server.MapPath(strname));
command.Parameters.Add(@ imageone,SqlDbType.VarChar,100).Value = strname;
// UPLOAD IMAGE 2 //
string strname2 = picture2.FileName.ToString();
strname2 =〜//品牌//新文件夹//+ strname2;
picture2.PostedFile.SaveAs(Server.MapPath(strname2));
command.Parameters.Add(@ imagetwo,SqlDbType.VarChar,100).Value = strname2;
//上传图片3 //
string strname3 = picture3.FileName.ToString();
strname3 =〜//品牌//新文件夹//+ strname3;
picture3.PostedFile.SaveAs(Server.MapPath(strname3));
command.Parameters.Add(@ imagethree,SqlDbType.VarChar,100).Value = strname3;



 protected void BindGrid ()
{

SqlDataAdapter myCommand = new SqlDataAdapter(SELECT * FROM pikiphones,con);
DataSet ds = new DataSet();
myCommand.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}



 protected void delete_rOW(object sender,GridViewDeleteEventArgs e)
{
String deleteCmd =DELETE来自pikiphones WHERE id = @id;
SqlCommand myCommand = new SqlCommand(deleteCmd,con);
myCommand.Parameters.Add(new SqlParameter(@ id,SqlDbType.Int));
myCommand.Parameters [@ id]。Value = Convert.ToInt32(GridView1.Rows [e.RowIndex] .Cells [1] .Text);
try
{
myCommand.ExecuteNonQuery();
}
catch(SqlException)
{
Label1.Text =错误:无法删除记录;

}
myCommand.Connection.Close();

BindGrid();
}
解决方案


I am able to delete from database but i do not know how delete image from folder.
Please help .
for better understanding please edit my code and paste.
Thanks in advance.

What I have tried:

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand command = new SqlCommand("SPinsertitem", con);
        command.CommandType = CommandType.StoredProcedure;
// UPLOAD IMAGE 1 //
        string strname = picture1.FileName.ToString();
        strname = "~//brand//New folder//" + strname;
        picture1.PostedFile.SaveAs(Server.MapPath(strname));
        command.Parameters.Add("@imageone", SqlDbType.VarChar, 100).Value = strname;
        // UPLOAD IMAGE 2 //
        string strname2 = picture2.FileName.ToString();
        strname2 = "~//brand//New folder//" + strname2;
        picture2.PostedFile.SaveAs(Server.MapPath(strname2));
        command.Parameters.Add("@imagetwo", SqlDbType.VarChar, 100).Value = strname2;
        // UPLOAD IMAGE 3 // 
        string strname3 = picture3.FileName.ToString();
        strname3 = "~//brand//New folder//" + strname3;
        picture3.PostedFile.SaveAs(Server.MapPath(strname3));
        command.Parameters.Add("@imagethree", SqlDbType.VarChar, 100).Value = strname3;


protected void BindGrid()
    {

        SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM pikiphones", con);
        DataSet ds = new DataSet();
        myCommand.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }


protected void delete_rOW(object sender, GridViewDeleteEventArgs e)
    {
        String deleteCmd = "DELETE FROM pikiphones WHERE id= @id";
        SqlCommand myCommand = new SqlCommand(deleteCmd, con);
        myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
        myCommand.Parameters["@id"].Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
        try
        {
            myCommand.ExecuteNonQuery();
        }
        catch (SqlException)
        {
            Label1.Text = "ERROR: Could not delete record";

        }
        myCommand.Connection.Close();
        
        BindGrid();
    }
解决方案



这篇关于使用ASP.NET C#中的gridview从文件夹和数据库中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 23:51